回答編集履歴
1
SubWindowの実装を別ファイルにした場合の実装例を追記
test
CHANGED
@@ -43,3 +43,103 @@
|
|
43
43
|
root.mainloop()
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
**【補足】**
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
SubWindowの実装を別ファイルにした場合の実装例
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
__main.py__
|
58
|
+
|
59
|
+
```Python
|
60
|
+
|
61
|
+
import tkinter as tk
|
62
|
+
|
63
|
+
import sub
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
root = tk.Tk()
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
def callback1(event):
|
72
|
+
|
73
|
+
if event.widget["state"] == "disabled":
|
74
|
+
|
75
|
+
return "break"
|
76
|
+
|
77
|
+
else:
|
78
|
+
|
79
|
+
event.widget["state"] = "disabled":
|
80
|
+
|
81
|
+
# SubWindowを開く
|
82
|
+
|
83
|
+
# 第1引数:SubWindowのowner/第2引数:SubWindowが閉じられたとき呼ばれる関数
|
84
|
+
|
85
|
+
sub.create_sub_window(root, lambda : Button1.config(state='normal'))
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
Button1 = tk.Button(root, text='SUB_WINDOW')
|
90
|
+
|
91
|
+
Button1.pack()
|
92
|
+
|
93
|
+
Button1.bind('<Button-1>', callback1)
|
94
|
+
|
95
|
+
root.mainloop()
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
__sub.py__
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
import tkinter as tk
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
sub = None
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def close_sub_window(on_destroy=None):
|
114
|
+
|
115
|
+
global sub
|
116
|
+
|
117
|
+
# 閉じたとき呼ばれる関数が設定されていたらCALLする
|
118
|
+
|
119
|
+
if on_destroy is not None:
|
120
|
+
|
121
|
+
on_destroy()
|
122
|
+
|
123
|
+
# SubWindowを閉じる
|
124
|
+
|
125
|
+
sub.destroy()
|
126
|
+
|
127
|
+
sub = None
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
def create_sub_window(owner, on_destroy=None):
|
132
|
+
|
133
|
+
global sub
|
134
|
+
|
135
|
+
# 既にSubWindowが開いていないかをチェック
|
136
|
+
|
137
|
+
if sub is not None:
|
138
|
+
|
139
|
+
return
|
140
|
+
|
141
|
+
sub = tk.Toplevel(owner)
|
142
|
+
|
143
|
+
sub.protocol('WM_DELETE_WINDOW', lambda: close_sub_window(on_destroy))
|
144
|
+
|
145
|
+
```
|