我正在使用带有顶级菜单栏的堆叠框架。菜单确实按预期显示。但是,我必须将焦点移开,然后再次返回以使菜单栏与我的活动框架聚焦。从那里开始,菜单栏就可以正常工作了。
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
self.winfo_toplevel().title("Sample App")
self.config(takefocus='true')
self.resizable(width=False, height=False)
self.maxsize(800, 600)
self.minsize(800, 600)
container.pack(side="left", fill="y", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
menubar = tk.Menu(self)
filemenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label ='File', menu=filemenu)
filemenu.add_command(label="Login", command=lambda:self.show_frame(Login))
filemenu.add_command(label="Logout", command=self.destroy)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.destroy)
help_ = tk.Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
self.config(menu=menubar)
self.frames = {}
for F in (Start, Page1, Page2):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=10, sticky="nsew")
self.show_frame(Start)
if platform() == 'Darwin': # How Mac OS X is identified by Python
system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "python" to true' ''')
self.focus_force()
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()