質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -355,3 +355,195 @@
|
|
355
355
|
|
356
356
|
|
357
357
|
どうか、このようなケースでスマートに処理を分ける方法がありましたら、ご教示ください。
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
### 2019/05/20追記
|
362
|
+
|
363
|
+
ご回答ありがとうございました。
|
364
|
+
|
365
|
+
コードを簡略化しすぎていたため、分かりにくい質問となってしまったことをお詫びします。
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
ご回答を参考に、一旦、クラス内クラスを使うこととしました。
|
370
|
+
|
371
|
+
以下のコードは、「左のメニューのボタンをクリックすると、RobotAとRobotBのコントロールパネルを表示する」というサンプルコードです。
|
372
|
+
|
373
|
+
```Python
|
374
|
+
|
375
|
+
import tkinter
|
376
|
+
|
377
|
+
import datetime
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
class Test:
|
382
|
+
|
383
|
+
def __init__(self):
|
384
|
+
|
385
|
+
# ウインドウの作成
|
386
|
+
|
387
|
+
self.root = tkinter.Tk()
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
# 大まかなフレームを作成
|
392
|
+
|
393
|
+
self.root_menu = tkinter.Frame(self.root)
|
394
|
+
|
395
|
+
self.root_menu.grid(column=0, row=0, sticky=tkinter.NSEW)
|
396
|
+
|
397
|
+
self.root_contents = tkinter.Frame(self.root)
|
398
|
+
|
399
|
+
self.root_contents.grid(column=1, row=0, sticky=tkinter.NSEW)
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
# root_contentsに表示されるframe
|
404
|
+
|
405
|
+
self.select_contents = None
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
# root_contentsがリサイズされるようにする
|
410
|
+
|
411
|
+
self.root.columnconfigure(1, weight=1)
|
412
|
+
|
413
|
+
self.root.rowconfigure(1, weight=0)
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
# メニューを作成
|
418
|
+
|
419
|
+
self.root_menu_buttons = {}
|
420
|
+
|
421
|
+
for x in dir(Test):
|
422
|
+
|
423
|
+
if x[0:4] == "Draw":
|
424
|
+
|
425
|
+
item = tkinter.Button(self.root_menu, text=x[4:], command=self.selector(x[4:]))
|
426
|
+
|
427
|
+
item.pack()
|
428
|
+
|
429
|
+
self.root_menu_buttons[x[4:]] = item
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
# 子項目に渡すframeを作成
|
434
|
+
|
435
|
+
self.contents_frames = {}
|
436
|
+
|
437
|
+
for x in self.root_menu_buttons:
|
438
|
+
|
439
|
+
self.contents_frames[x] = tkinter.Frame(self.root_contents)
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
# 子項目をインスタンス化
|
444
|
+
|
445
|
+
self.contents_instance = {}
|
446
|
+
|
447
|
+
for x in self.root_menu_buttons:
|
448
|
+
|
449
|
+
exec("self.contents_instance[x] = self.Draw" + x + "(self, self.contents_frames[x])")
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
self.update()
|
456
|
+
|
457
|
+
self.root.mainloop()
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
def selector(self, select):
|
462
|
+
|
463
|
+
def func():
|
464
|
+
|
465
|
+
# 表示されているものを隠す
|
466
|
+
|
467
|
+
if self.select_contents is not None:
|
468
|
+
|
469
|
+
self.select_contents.pack_forget()
|
470
|
+
|
471
|
+
print("隠す")
|
472
|
+
|
473
|
+
# 選択されたウィジェットを表示する。
|
474
|
+
|
475
|
+
self.contents_frames[select].pack(fill=tkinter.BOTH)
|
476
|
+
|
477
|
+
self.select_contents = self.contents_frames[select]
|
478
|
+
|
479
|
+
return func
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
def update(self):
|
484
|
+
|
485
|
+
for x in self.root_menu_buttons:
|
486
|
+
|
487
|
+
self.contents_instance[x].update()
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
self.root.after(1000, self.update)
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
# RobotAとRobotBは全く別の製品で処理の共通化は必要ない。
|
496
|
+
|
497
|
+
class DrawRobotA:
|
498
|
+
|
499
|
+
def __init__(self, self_, root):
|
500
|
+
|
501
|
+
self.root = root
|
502
|
+
|
503
|
+
tkinter.Label(root, text="RobotAのコントロールパネル").pack()
|
504
|
+
|
505
|
+
self.message = tkinter.Label(root, text="RobotA")
|
506
|
+
|
507
|
+
self.message.pack()
|
508
|
+
|
509
|
+
# 以下、RobotB固有の複数のwidgetが並ぶ
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
def update(self):
|
514
|
+
|
515
|
+
self.message["text"] = "RobotA" + str(datetime.datetime.now())
|
516
|
+
|
517
|
+
|
518
|
+
|
519
|
+
class DrawRobotB:
|
520
|
+
|
521
|
+
def __init__(self, self_, root):
|
522
|
+
|
523
|
+
self.root = root
|
524
|
+
|
525
|
+
tkinter.Label(root, text="RobotBのコントロールパネル").pack()
|
526
|
+
|
527
|
+
self.message = tkinter.Label(root, text="RobotB")
|
528
|
+
|
529
|
+
self.message.pack()
|
530
|
+
|
531
|
+
# 以下、RobotB固有の複数のwidgetが並ぶ
|
532
|
+
|
533
|
+
|
534
|
+
|
535
|
+
def update(self):
|
536
|
+
|
537
|
+
self.message["text"] = "RobotB" + str(datetime.datetime.now())
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
Test()
|
542
|
+
|
543
|
+
|
544
|
+
|
545
|
+
```
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
一応解決となりましたので、様子をみてベストアンサーを決定してクローズとさせていただきます。
|