Tkinter菜单栏似乎并不专注于macOS

问题描述

我正在使用带有顶层菜单栏的堆叠框架。菜单确实按预期显示。但是,我必须将焦点移开,然后再次返回,以使菜单栏与活动框架一起聚焦。从那里开始,菜单栏可以正常工作。

    class App(tk.Tk):
        def __init__(self,*args,**kwargs):
            tk.Tk.__init__(self,**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()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)