teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

説明追加

2020/05/18 05:34

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -1,1 +1,145 @@
1
+ 下記のリンクに同様の質問がありました。
2
+ こちらの1つ目の回答の方法で良いかと思います。
3
+
1
- [https://stackoverflow.com/questions/1450180/change-the-focus-from-one-text-widget-to-another](https://stackoverflow.com/questions/1450180/change-the-focus-from-one-text-widget-to-another)
4
+ [https://stackoverflow.com/questions/1450180/change-the-focus-from-one-text-widget-to-another](https://stackoverflow.com/questions/1450180/change-the-focus-from-one-text-widget-to-another)
5
+
6
+ ```Python
7
+ from tkinter import *
8
+ import tkinter.ttk as ttk
9
+ import tkinter.scrolledtext as tksc
10
+ import math
11
+
12
+ class Apprication(ttk.Frame):
13
+
14
+ def __init__(self, app):
15
+ super().__init__(app)
16
+ self.pack()
17
+
18
+ btn = ttk.Button(self, text="Sub", command=self.openDialog)
19
+ btn.grid(row=1, column=0)
20
+
21
+ def focus_next_window(self, event):
22
+ event.widget.tk_focusNext().focus()
23
+ return "break"
24
+
25
+ # 子画面開く
26
+ def openDialog(self):
27
+
28
+ self.dialog = Toplevel(self)
29
+ self.dialog.title("Sub Menu")
30
+
31
+ #フォームサイズを実行端末から導き、ド真中に配置表示
32
+ lw = math.ceil(ww * 0.408)
33
+ lh = math.ceil(wh * 0.477)
34
+ self.dialog.geometry(str(lw)+"x"+str(lh)+"+"+str(int(ww/2-lw/2))+"+"+str(int(wh/2-lh/2)) )
35
+
36
+ self.dialog.configure(bg="#F0FFFF")
37
+ self.dialog.resizable(0,0)
38
+ self.dialog.protocol('WM_DELETE_WINDOW', (lambda: 'pass')())
39
+
40
+ # 当該ダイアログのカーソルを変更し、関数側でもカーソルを変更できるように
41
+ self.dialog['cursor'] = 'hand2'
42
+ self.this = self.dialog
43
+
44
+ # modalに
45
+ self.dialog.grab_set()
46
+
47
+ # コンボボックス
48
+ db = {1:"AAA",2:"BBB",3:"CCC"}
49
+ self.v1 = StringVar()
50
+ cmbox1 = ttk.Combobox(self.dialog, takefocus=1, width=5, justify=CENTER, values=list(db.keys()), state='readonly', textvariable=self.v1)
51
+ cmbox1.bind('<<ComboboxSelected>>', self.cmbox1_selected)
52
+ cmbox1.grid(row=0, column=0, padx=(10, 0), pady=(10,0), sticky=W+E)
53
+ cmbox1.focus_set()
54
+
55
+
56
+ # テキストボックス
57
+ self.txt1 = Entry(self.dialog, state="readonly", takefocus=1)
58
+ self.txt1.grid(row=0, column=1, columnspan=7, sticky=W+E, pady=(10,0))
59
+
60
+
61
+ # ★入力枠★
62
+ self.scrtxt1 = tksc.ScrolledText(self.dialog, bg="black", fg="orange", font=("Helvetica",11), insertbackground="orange", blockcursor=True, height=6, state="disable", takefocus=1)
63
+ self.scrtxt1.bind("<Leave>", self.scrtxt1_Chk)
64
+ self.scrtxt1.grid(row=2, column=0, columnspan=11, sticky=W+E, padx=10)
65
+ self.scrtxt1.bind("<Tab>", self.focus_next_window)
66
+
67
+ # 実行ボタン
68
+ self.btn1 = Button(self.dialog, text='Execute', width=10, state=DISABLED, takefocus=1)
69
+ self.btn1.grid(row=3, columnspan=11, pady=(0, 20), sticky=N)
70
+
71
+
72
+ # 閉じるボタン
73
+ btn3 = Button(self.dialog, text='Quit', command=self.closeDialog, width=10, takefocus=1)
74
+ btn3.grid(row=5, column=10, pady=10, padx=(0,10))
75
+
76
+
77
+ self.dialog.grid_rowconfigure(1, weight=1)
78
+ self.dialog.grid_rowconfigure(3, weight=1)
79
+ self.dialog.grid_columnconfigure(2, weight=1)
80
+
81
+
82
+
83
+
84
+ # 子画面閉じる
85
+ def closeDialog(self):
86
+ self.dialog.destroy()
87
+
88
+
89
+ # コンボボックス選択値をテキストボックス表示
90
+ def cmbox1_selected(self, event):
91
+ self.txt1['state'] = 'normal'
92
+ self.txt1.delete(0, END)
93
+ self.txt1.insert(END, self.v1.get())
94
+ self.txt1['state'] = 'readonly'
95
+
96
+ self.scrtxt1['state'] = 'normal'
97
+ self.btn1_Enable()
98
+
99
+
100
+ # ★★★入力枠チェック★★★
101
+ def scrtxt1_Chk(self, event):
102
+ self.btn1_Enable()
103
+
104
+
105
+ # 実行ボタンの有効化
106
+ def btn1_Enable(self):
107
+ if (self.scrtxt1.get('1.0', 'end -1c') != ""):
108
+ self.btn1['state'] = 'normal'
109
+ else:
110
+ self.btn1['state'] = 'disable'
111
+
112
+
113
+ if __name__ == '__main__':
114
+
115
+ #世間でいうrootをappとしています
116
+ app = Tk()
117
+
118
+ #実行端末の画面サイズを取得
119
+ ww = app.winfo_screenwidth()
120
+ wh = app.winfo_screenheight()
121
+
122
+ app.update_idletasks()
123
+
124
+ #フォームサイズを実行端末から導き、ド真中に配置表示
125
+ lw = math.ceil(ww * 0.208)
126
+ lh = math.ceil(wh * 0.277)
127
+ app.geometry(str(lw)+"x"+str(lh)+"+"+str(int(ww/2-lw/2))+"+"+str(int(wh/2-lh/2)) )
128
+
129
+ #タイトルを指定
130
+ app.title("Main Menu")
131
+
132
+ #フォームの最大化、×ボタン操作を無効化
133
+ app.resizable(0,0)
134
+ #app.protocol('WM_DELETE_WINDOW', (lambda: 'pass')())
135
+
136
+ # カーソル変更
137
+ app["cursor"] = "hand2"
138
+
139
+ app.configure(bg="#F0FFFF")
140
+
141
+ # フレームを作成する
142
+ frame = Apprication(app)
143
+ # 格納したTkインスタンスのmainloopで画面を起こす
144
+ app.mainloop()
145
+ ```