質問編集履歴
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -176,4 +176,100 @@
|
|
176
176
|
```
|
177
177
|
・・・前の2つよりは改善された気がしますが、ネストが深くなってしまった気がします。
|
178
178
|
|
179
|
-
どうか、このようなケースでスマートに処理を分ける方法がありましたら、ご教示ください。
|
179
|
+
どうか、このようなケースでスマートに処理を分ける方法がありましたら、ご教示ください。
|
180
|
+
|
181
|
+
### 2019/05/20追記
|
182
|
+
ご回答ありがとうございました。
|
183
|
+
コードを簡略化しすぎていたため、分かりにくい質問となってしまったことをお詫びします。
|
184
|
+
|
185
|
+
ご回答を参考に、一旦、クラス内クラスを使うこととしました。
|
186
|
+
以下のコードは、「左のメニューのボタンをクリックすると、RobotAとRobotBのコントロールパネルを表示する」というサンプルコードです。
|
187
|
+
```Python
|
188
|
+
import tkinter
|
189
|
+
import datetime
|
190
|
+
|
191
|
+
class Test:
|
192
|
+
def __init__(self):
|
193
|
+
# ウインドウの作成
|
194
|
+
self.root = tkinter.Tk()
|
195
|
+
|
196
|
+
# 大まかなフレームを作成
|
197
|
+
self.root_menu = tkinter.Frame(self.root)
|
198
|
+
self.root_menu.grid(column=0, row=0, sticky=tkinter.NSEW)
|
199
|
+
self.root_contents = tkinter.Frame(self.root)
|
200
|
+
self.root_contents.grid(column=1, row=0, sticky=tkinter.NSEW)
|
201
|
+
|
202
|
+
# root_contentsに表示されるframe
|
203
|
+
self.select_contents = None
|
204
|
+
|
205
|
+
# root_contentsがリサイズされるようにする
|
206
|
+
self.root.columnconfigure(1, weight=1)
|
207
|
+
self.root.rowconfigure(1, weight=0)
|
208
|
+
|
209
|
+
# メニューを作成
|
210
|
+
self.root_menu_buttons = {}
|
211
|
+
for x in dir(Test):
|
212
|
+
if x[0:4] == "Draw":
|
213
|
+
item = tkinter.Button(self.root_menu, text=x[4:], command=self.selector(x[4:]))
|
214
|
+
item.pack()
|
215
|
+
self.root_menu_buttons[x[4:]] = item
|
216
|
+
|
217
|
+
# 子項目に渡すframeを作成
|
218
|
+
self.contents_frames = {}
|
219
|
+
for x in self.root_menu_buttons:
|
220
|
+
self.contents_frames[x] = tkinter.Frame(self.root_contents)
|
221
|
+
|
222
|
+
# 子項目をインスタンス化
|
223
|
+
self.contents_instance = {}
|
224
|
+
for x in self.root_menu_buttons:
|
225
|
+
exec("self.contents_instance[x] = self.Draw" + x + "(self, self.contents_frames[x])")
|
226
|
+
|
227
|
+
|
228
|
+
self.update()
|
229
|
+
self.root.mainloop()
|
230
|
+
|
231
|
+
def selector(self, select):
|
232
|
+
def func():
|
233
|
+
# 表示されているものを隠す
|
234
|
+
if self.select_contents is not None:
|
235
|
+
self.select_contents.pack_forget()
|
236
|
+
print("隠す")
|
237
|
+
# 選択されたウィジェットを表示する。
|
238
|
+
self.contents_frames[select].pack(fill=tkinter.BOTH)
|
239
|
+
self.select_contents = self.contents_frames[select]
|
240
|
+
return func
|
241
|
+
|
242
|
+
def update(self):
|
243
|
+
for x in self.root_menu_buttons:
|
244
|
+
self.contents_instance[x].update()
|
245
|
+
|
246
|
+
self.root.after(1000, self.update)
|
247
|
+
|
248
|
+
# RobotAとRobotBは全く別の製品で処理の共通化は必要ない。
|
249
|
+
class DrawRobotA:
|
250
|
+
def __init__(self, self_, root):
|
251
|
+
self.root = root
|
252
|
+
tkinter.Label(root, text="RobotAのコントロールパネル").pack()
|
253
|
+
self.message = tkinter.Label(root, text="RobotA")
|
254
|
+
self.message.pack()
|
255
|
+
# 以下、RobotB固有の複数のwidgetが並ぶ
|
256
|
+
|
257
|
+
def update(self):
|
258
|
+
self.message["text"] = "RobotA" + str(datetime.datetime.now())
|
259
|
+
|
260
|
+
class DrawRobotB:
|
261
|
+
def __init__(self, self_, root):
|
262
|
+
self.root = root
|
263
|
+
tkinter.Label(root, text="RobotBのコントロールパネル").pack()
|
264
|
+
self.message = tkinter.Label(root, text="RobotB")
|
265
|
+
self.message.pack()
|
266
|
+
# 以下、RobotB固有の複数のwidgetが並ぶ
|
267
|
+
|
268
|
+
def update(self):
|
269
|
+
self.message["text"] = "RobotB" + str(datetime.datetime.now())
|
270
|
+
|
271
|
+
Test()
|
272
|
+
|
273
|
+
```
|
274
|
+
|
275
|
+
一応解決となりましたので、様子をみてベストアンサーを決定してクローズとさせていただきます。
|