回答編集履歴
2
誤記
test
CHANGED
@@ -124,11 +124,11 @@
|
|
124
124
|
|
125
125
|
|
126
126
|
|
127
|
-
MyFrameの画面構成を決めているのはMyFrameの`__init__`メソッドですが、MyFrame
|
127
|
+
MyFrameの画面構成を決めているのはMyFrameの`__init__`メソッドですが、MyFrameのレイアウト方法は次のいずれかの方針とすべきです。(gridも選択肢になりますがその方法については言及しません)
|
128
|
-
|
129
|
-
|
130
|
-
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
-
(1) MyFrameの大きさを明示的に指定しておき、その上で各Buttonをplaceで配置
|
131
|
+
(1) MyFrameの大きさを明示的に指定しておき、その上で各Buttonなどをplaceで配置
|
132
132
|
|
133
133
|
(2) 子供をpackで配置してMyFrameの大きさは子供の位置や大きさに応じて自動的に決める
|
134
134
|
|
1
追記
test
CHANGED
@@ -41,3 +41,163 @@
|
|
41
41
|
|
42
42
|
|
43
43
|
すみませんが、今、自分で確認する時間がないので違ってたらごめんなさい。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
---
|
48
|
+
|
49
|
+
追記:
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
実際にやってみましたが元の回答で書いたことは正しそうです。しかしながらご質問のコードは一部しか書かれておらず、書き方によって上記の問題以前に**ボタン自体が表示されない**という不具合がおきそうです。以下のコードで確認してみました。
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
```Python
|
58
|
+
|
59
|
+
import tkinter as tk
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
class MyFrame(tk.Frame):
|
66
|
+
|
67
|
+
# 問題とは無関係な冗長な記述をより短い記述に書き換えてます
|
68
|
+
|
69
|
+
def __init__(self, parent, controller):
|
70
|
+
|
71
|
+
tk.Frame.__init__(self, parent, bg='#014cc4')
|
72
|
+
|
73
|
+
self.controller = controller
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
switch_button_image = tk.PhotoImage(file="./switch.png")
|
78
|
+
|
79
|
+
button_Sw = tk.Button(self, image=switch_button_image, bg="white", width=40, height=40)
|
80
|
+
|
81
|
+
button_Sw.pack()
|
82
|
+
|
83
|
+
button_Sw.place(x=19, y=10)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
setting_button_image = tk.PhotoImage(file="./setting.png")
|
88
|
+
|
89
|
+
button_Se = tk.Button(self, image=setting_button_image, bg="white", width=40, height=40)
|
90
|
+
|
91
|
+
button_Se.pack()
|
92
|
+
|
93
|
+
button_Se.place(x=250, y=10)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
def report(ev):
|
100
|
+
|
101
|
+
w = ev.widget
|
102
|
+
|
103
|
+
print(f'widget:{w}, width={w.winfo_width()}, height={w.winfo_height()}')
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
root = tk.Tk()
|
110
|
+
|
111
|
+
root.bind_all('<Configure>', report)
|
112
|
+
|
113
|
+
root.geometry('400x400')
|
114
|
+
|
115
|
+
frame = MyFrame(root, object())
|
116
|
+
|
117
|
+
frame.pack()
|
118
|
+
|
119
|
+
root.mainloop()
|
120
|
+
|
121
|
+
```
|
122
|
+
|
123
|
+
上記を実行するとMyFrameの大きさは1x1にしかならず、配置した2つのボタンはMyFrameの領域外に位置付けられてしまいボタン自体が見えなくなることがわかります。こうなる原因はウィジェットのレイアウト方法がおかしいからです。
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
MyFrameの画面構成を決めているのはMyFrameの`__init__`メソッドですが、MyFrameがどのような大きさになるべきかは次のいずれかの方針とすべきです。(gridも選択肢になりますその方法については言及しません)
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
(1) MyFrameの大きさを明示的に指定しておき、その上で各Buttonをplaceで配置
|
132
|
+
|
133
|
+
(2) 子供をpackで配置してMyFrameの大きさは子供の位置や大きさに応じて自動的に決める
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
元のコードでは(1),(2)のどちらでもなくpackとplaceを両方使ってます。placeを呼び出すとpackの効果はなくなりますのでpackしてないことと同義になります。
|
138
|
+
|
139
|
+
レイアウトのしかたは慣れないと混乱しがちです。例えば
|
140
|
+
|
141
|
+
[http://www.shido.info/py/tkinter2.html](http://www.shido.info/py/tkinter2.html)
|
142
|
+
|
143
|
+
のような解説をよく読むとよいとおもいます。
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
---
|
148
|
+
|
149
|
+
「なぜうまくいかないか」について説明を試みましたがちょっと難しいかも知れませんので、最後に「うまく表示される簡単なコード例」を示しておきます。
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
```python
|
154
|
+
|
155
|
+
import tkinter as tk
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
class MyFrame(tk.Frame):
|
162
|
+
|
163
|
+
def __init__(self, parent, controller):
|
164
|
+
|
165
|
+
# Frameのサイズを明示的に指定する
|
166
|
+
|
167
|
+
tk.Frame.__init__(self, parent, width=400, height=400, bg='#014cc4')
|
168
|
+
|
169
|
+
self.controller = controller
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
switch_button_image = tk.PhotoImage(file="./switch.png")
|
174
|
+
|
175
|
+
button_Sw = tk.Button(self, image=switch_button_image, bg="white", width=40, height=40)
|
176
|
+
|
177
|
+
button_Sw.place(x=19, y=10)
|
178
|
+
|
179
|
+
self.switch_button_image = switch_button_image # 画像インスタンスの参照を覚えておく
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
setting_button_image = tk.PhotoImage(file="./setting.png")
|
184
|
+
|
185
|
+
button_Se = tk.Button(self, image=setting_button_image, bg="white", width=40, height=40)
|
186
|
+
|
187
|
+
button_Se.place(x=250, y=10)
|
188
|
+
|
189
|
+
self.setting_button_image = setting_button_image # 画像インスタンスの参照を覚えておく
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
root = tk.Tk()
|
196
|
+
|
197
|
+
frame = MyFrame(root, object())
|
198
|
+
|
199
|
+
frame.pack()
|
200
|
+
|
201
|
+
root.mainloop()
|
202
|
+
|
203
|
+
```
|