回答編集履歴
1
説明追加
test
CHANGED
@@ -1 +1,289 @@
|
|
1
|
+
下記のリンクに同様の質問がありました。
|
2
|
+
|
3
|
+
こちらの1つ目の回答の方法で良いかと思います。
|
4
|
+
|
5
|
+
|
6
|
+
|
1
7
|
[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)
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
```Python
|
12
|
+
|
13
|
+
from tkinter import *
|
14
|
+
|
15
|
+
import tkinter.ttk as ttk
|
16
|
+
|
17
|
+
import tkinter.scrolledtext as tksc
|
18
|
+
|
19
|
+
import math
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
class Apprication(ttk.Frame):
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
def __init__(self, app):
|
28
|
+
|
29
|
+
super().__init__(app)
|
30
|
+
|
31
|
+
self.pack()
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
btn = ttk.Button(self, text="Sub", command=self.openDialog)
|
36
|
+
|
37
|
+
btn.grid(row=1, column=0)
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
def focus_next_window(self, event):
|
42
|
+
|
43
|
+
event.widget.tk_focusNext().focus()
|
44
|
+
|
45
|
+
return "break"
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
# 子画面開く
|
50
|
+
|
51
|
+
def openDialog(self):
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
self.dialog = Toplevel(self)
|
56
|
+
|
57
|
+
self.dialog.title("Sub Menu")
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
#フォームサイズを実行端末から導き、ド真中に配置表示
|
62
|
+
|
63
|
+
lw = math.ceil(ww * 0.408)
|
64
|
+
|
65
|
+
lh = math.ceil(wh * 0.477)
|
66
|
+
|
67
|
+
self.dialog.geometry(str(lw)+"x"+str(lh)+"+"+str(int(ww/2-lw/2))+"+"+str(int(wh/2-lh/2)) )
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
self.dialog.configure(bg="#F0FFFF")
|
72
|
+
|
73
|
+
self.dialog.resizable(0,0)
|
74
|
+
|
75
|
+
self.dialog.protocol('WM_DELETE_WINDOW', (lambda: 'pass')())
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
# 当該ダイアログのカーソルを変更し、関数側でもカーソルを変更できるように
|
80
|
+
|
81
|
+
self.dialog['cursor'] = 'hand2'
|
82
|
+
|
83
|
+
self.this = self.dialog
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
# modalに
|
88
|
+
|
89
|
+
self.dialog.grab_set()
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
# コンボボックス
|
94
|
+
|
95
|
+
db = {1:"AAA",2:"BBB",3:"CCC"}
|
96
|
+
|
97
|
+
self.v1 = StringVar()
|
98
|
+
|
99
|
+
cmbox1 = ttk.Combobox(self.dialog, takefocus=1, width=5, justify=CENTER, values=list(db.keys()), state='readonly', textvariable=self.v1)
|
100
|
+
|
101
|
+
cmbox1.bind('<<ComboboxSelected>>', self.cmbox1_selected)
|
102
|
+
|
103
|
+
cmbox1.grid(row=0, column=0, padx=(10, 0), pady=(10,0), sticky=W+E)
|
104
|
+
|
105
|
+
cmbox1.focus_set()
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
# テキストボックス
|
112
|
+
|
113
|
+
self.txt1 = Entry(self.dialog, state="readonly", takefocus=1)
|
114
|
+
|
115
|
+
self.txt1.grid(row=0, column=1, columnspan=7, sticky=W+E, pady=(10,0))
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
# ★入力枠★
|
122
|
+
|
123
|
+
self.scrtxt1 = tksc.ScrolledText(self.dialog, bg="black", fg="orange", font=("Helvetica",11), insertbackground="orange", blockcursor=True, height=6, state="disable", takefocus=1)
|
124
|
+
|
125
|
+
self.scrtxt1.bind("<Leave>", self.scrtxt1_Chk)
|
126
|
+
|
127
|
+
self.scrtxt1.grid(row=2, column=0, columnspan=11, sticky=W+E, padx=10)
|
128
|
+
|
129
|
+
self.scrtxt1.bind("<Tab>", self.focus_next_window)
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
# 実行ボタン
|
134
|
+
|
135
|
+
self.btn1 = Button(self.dialog, text='Execute', width=10, state=DISABLED, takefocus=1)
|
136
|
+
|
137
|
+
self.btn1.grid(row=3, columnspan=11, pady=(0, 20), sticky=N)
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
# 閉じるボタン
|
144
|
+
|
145
|
+
btn3 = Button(self.dialog, text='Quit', command=self.closeDialog, width=10, takefocus=1)
|
146
|
+
|
147
|
+
btn3.grid(row=5, column=10, pady=10, padx=(0,10))
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
self.dialog.grid_rowconfigure(1, weight=1)
|
154
|
+
|
155
|
+
self.dialog.grid_rowconfigure(3, weight=1)
|
156
|
+
|
157
|
+
self.dialog.grid_columnconfigure(2, weight=1)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
# 子画面閉じる
|
168
|
+
|
169
|
+
def closeDialog(self):
|
170
|
+
|
171
|
+
self.dialog.destroy()
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
# コンボボックス選択値をテキストボックス表示
|
178
|
+
|
179
|
+
def cmbox1_selected(self, event):
|
180
|
+
|
181
|
+
self.txt1['state'] = 'normal'
|
182
|
+
|
183
|
+
self.txt1.delete(0, END)
|
184
|
+
|
185
|
+
self.txt1.insert(END, self.v1.get())
|
186
|
+
|
187
|
+
self.txt1['state'] = 'readonly'
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
self.scrtxt1['state'] = 'normal'
|
192
|
+
|
193
|
+
self.btn1_Enable()
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
# ★★★入力枠チェック★★★
|
200
|
+
|
201
|
+
def scrtxt1_Chk(self, event):
|
202
|
+
|
203
|
+
self.btn1_Enable()
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# 実行ボタンの有効化
|
210
|
+
|
211
|
+
def btn1_Enable(self):
|
212
|
+
|
213
|
+
if (self.scrtxt1.get('1.0', 'end -1c') != ""):
|
214
|
+
|
215
|
+
self.btn1['state'] = 'normal'
|
216
|
+
|
217
|
+
else:
|
218
|
+
|
219
|
+
self.btn1['state'] = 'disable'
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
if __name__ == '__main__':
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
#世間でいうrootをappとしています
|
230
|
+
|
231
|
+
app = Tk()
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
#実行端末の画面サイズを取得
|
236
|
+
|
237
|
+
ww = app.winfo_screenwidth()
|
238
|
+
|
239
|
+
wh = app.winfo_screenheight()
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
app.update_idletasks()
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
#フォームサイズを実行端末から導き、ド真中に配置表示
|
248
|
+
|
249
|
+
lw = math.ceil(ww * 0.208)
|
250
|
+
|
251
|
+
lh = math.ceil(wh * 0.277)
|
252
|
+
|
253
|
+
app.geometry(str(lw)+"x"+str(lh)+"+"+str(int(ww/2-lw/2))+"+"+str(int(wh/2-lh/2)) )
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
#タイトルを指定
|
258
|
+
|
259
|
+
app.title("Main Menu")
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
#フォームの最大化、×ボタン操作を無効化
|
264
|
+
|
265
|
+
app.resizable(0,0)
|
266
|
+
|
267
|
+
#app.protocol('WM_DELETE_WINDOW', (lambda: 'pass')())
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
# カーソル変更
|
272
|
+
|
273
|
+
app["cursor"] = "hand2"
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
app.configure(bg="#F0FFFF")
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
# フレームを作成する
|
282
|
+
|
283
|
+
frame = Apprication(app)
|
284
|
+
|
285
|
+
# 格納したTkインスタンスのmainloopで画面を起こす
|
286
|
+
|
287
|
+
app.mainloop()
|
288
|
+
|
289
|
+
```
|