2017-08-18 1 views
1

Hallo devs、 フレームページメニューカスケードのonclickの変更方法は? はコメント:#、コマンド=ラムダ:controller.show_frame(インタフェース))Python GUIメニューに別のフレームをロードする

import tkinter as tk 
import config.window_config as conf 
from pages.interface import Interface 
from pages.settings import Settings 


class network_tools(tk.Tk): 

    def __init__(self, *args, **kwargs): 

     tk.Tk.__init__(self, *args, **kwargs) 
     container = tk.Frame(self) 

     self.title(conf.title) 
     self.geometry(conf.geometry) 

     # menu 
     menubar = tk.Menu(self) 

     # menu pattern 
     #** here is the problem ** 
     interface = tk.Menu(menubar, tearoff=0)#, command=lambda: controller.show_frame(Interface)) 
     settings = tk.Menu(menubar, tearoff=0)#, command=lambda: controller.show_frame(Settings)) 

     menubar.add_cascade(label="Interface", menu=interface) 
     menubar.add_cascade(label="Settings", menu=settings) 
     self.config(menu=menubar) 

     container.pack(side="top", fill="both", expand = True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     #** frames loading ** 
     self.frames = {} 

     for F in (Interface, Settings): 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid(row=0, column=0, stick="nsew") 

     # default upstart frame 
     self.show_frame(Interface) 

    def show_frame(self, cont): 
     frame = self.frames[cont] 
     frame.tkraise() 

app = network_tools() 
app.mainloop() 
+0

"__init__ インターフェース= tk.Menu(メニューバー、切取= 0、コマンド=ラムダで" エラーが、素敵な試み "" /パイソン/ network_tools/network_tools.py」、行21、: self.show_frame(self.frames [Interface])) – Bitfighter

+0

プロジェクト全体をアップロードします。 – Bitfighter

+0

https://github.com/KinLux/network-tools – Bitfighter

答えて

1

カスケードメニューパターンは以下のようなものであるべきである。

この例では、一つにInterfaceにアクセスする2つのメニューを定義し他でSettings

menubar = tk.Menu(self) 

interface = tk.Menu(menubar, tearoff=0) 
settings = tk.Menu(menubar, tearoff=0) 

interface.add_command(label="Interface", command=lambda: self.show_frame(Interface)) 
menubar.add_cascade(label='Interface', menu=interface) 

settings.add_command(label="Settings", command=lambda: self.show_frame(Settings)) 
menubar.add_cascade(label='Settings', menu=settings) 
self.config(menu=menubar) 

そして、この例では、そのひだのみ1メニューを使用しています2つのフレーム:

menubar = tk.Menu(self) 

# menu pattern 

screens = tk.Menu(menubar, tearoff=0) 

screens.add_command(label="Interface", command=lambda: self.show_frame(Interface)) 
screens.add_command(label="Settings", command=lambda: self.show_frame(Settings)) 
menubar.add_cascade(label='Screens', menu=screens) 

self.config(menu=menubar) 
関連する問題