回答編集履歴

6

インスタンス化しない方法を追記

2020/02/04 00:21

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -101,3 +101,77 @@
101
101
  app.mainloop()
102
102
 
103
103
  ```
104
+
105
+
106
+
107
+ インスタンス化しないで使う方法。
108
+
109
+
110
+
111
+ ```python
112
+
113
+ import tkinter as tk
114
+
115
+
116
+
117
+
118
+
119
+ class Application(tk.Frame):
120
+
121
+ def __init__(self, master=None):
122
+
123
+ super().__init__(master)
124
+
125
+ self.pack()
126
+
127
+
128
+
129
+ master.geometry("300x300")
130
+
131
+ debug = tk.Button(text="サブウインドウの形成", command=Screen.display)
132
+
133
+ debug.pack()
134
+
135
+
136
+
137
+
138
+
139
+ class Screen:
140
+
141
+ # ディスプレイの表示部分
142
+
143
+ @staticmethod
144
+
145
+ def display():
146
+
147
+ sub = tk.Toplevel(root)
148
+
149
+ sub.geometry('200x200')
150
+
151
+
152
+
153
+ wbtn = tk.Button(sub, text='関数の実行', command=Screen.test)
154
+
155
+ wbtn.pack()
156
+
157
+
158
+
159
+ @staticmethod
160
+
161
+ def test():
162
+
163
+ print('OK!!!')
164
+
165
+
166
+
167
+
168
+
169
+ if __name__ == "__main__":
170
+
171
+ root = tk.Tk()
172
+
173
+ app = Application(master=root)
174
+
175
+ app.mainloop()
176
+
177
+ ```

5

不要なselfへの代入を削除

2020/02/04 00:21

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -58,9 +58,9 @@
58
58
 
59
59
  master.geometry("300x300")
60
60
 
61
- self.debug = tk.Button(text="サブウインドウの形成", command=Screen().display)
61
+ debug = tk.Button(text="サブウインドウの形成", command=Screen().display)
62
62
 
63
- self.debug.pack()
63
+ debug.pack()
64
64
 
65
65
 
66
66
 
@@ -78,9 +78,9 @@
78
78
 
79
79
 
80
80
 
81
- self.wbtn = tk.Button(sub, text='関数の実行', command=self.test)
81
+ wbtn = tk.Button(sub, text='関数の実行', command=self.test)
82
82
 
83
- self.wbtn.pack()
83
+ wbtn.pack()
84
84
 
85
85
 
86
86
 

4

説明変更

2020/02/03 08:52

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
 
26
26
 
27
- はインスタンスメソッドです。
27
+ のdisplayメソッドはインスタンスメソッドです。
28
28
 
29
29
  displayメソッドの self引数には Screenクラスのインスタンスが渡ってくることを期待していますが、実際には Eventクラスのインスタンスが渡ってきてしまいます。
30
30
 

3

うまく動くコードに変更

2020/02/03 08:22

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -34,28 +34,70 @@
34
34
 
35
35
 
36
36
 
37
- ひとまず、
37
+ プログラムを以下のように変更してみてください。
38
38
 
39
39
 
40
40
 
41
41
  ```python
42
42
 
43
- self.debug.bind("<1>", Screen.display)
43
+ import tkinter as tk
44
-
45
- ```
46
44
 
47
45
 
48
46
 
47
+
48
+
49
+ class Application(tk.Frame):
50
+
51
+ def __init__(self, master=None):
52
+
53
+ super().__init__(master)
54
+
49
-
55
+ self.pack()
50
56
 
51
57
 
52
58
 
53
- ```python
59
+ master.geometry("300x300")
54
60
 
55
- self.debug.bind("<1>", lambda event: Screen().display())
61
+ self.debug = tk.Button(text="サブウインドウの形成", command=Screen().display)
56
62
 
57
- ```
63
+ self.debug.pack()
58
64
 
59
65
 
60
66
 
67
+
68
+
69
+ class Screen:
70
+
71
+ # ディスプレイの表示部分
72
+
61
- にすればエラーは出なくなるでしょう。
73
+ def display(self):
74
+
75
+ sub = tk.Toplevel(root)
76
+
77
+ sub.geometry('200x200')
78
+
79
+
80
+
81
+ self.wbtn = tk.Button(sub, text='関数の実行', command=self.test)
82
+
83
+ self.wbtn.pack()
84
+
85
+
86
+
87
+ def test(self):
88
+
89
+ print('OK!!!')
90
+
91
+
92
+
93
+
94
+
95
+ if __name__ == "__main__":
96
+
97
+ root = tk.Tk()
98
+
99
+ app = Application(master=root)
100
+
101
+ app.mainloop()
102
+
103
+ ```

2

説明修正

2020/02/03 08:16

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -58,4 +58,4 @@
58
58
 
59
59
 
60
60
 
61
- にすればくでしょう。
61
+ にすればエラーは出ななるでしょう。

1

解決方法追記

2020/02/03 08:08

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -31,3 +31,31 @@
31
31
 
32
32
 
33
33
  クラスメソッドとインスタンスメソッドの違いについて調べてみるといいですよ。
34
+
35
+
36
+
37
+ ひとまず、
38
+
39
+
40
+
41
+ ```python
42
+
43
+ self.debug.bind("<1>", Screen.display)
44
+
45
+ ```
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+ ```python
54
+
55
+ self.debug.bind("<1>", lambda event: Screen().display())
56
+
57
+ ```
58
+
59
+
60
+
61
+ にすれば動くでしょう。