我想跟着设置一个应用,会有多个窗口我堆叠,然后只把被调用的那个窗口带到前面。它是基于这个系列的视频
https:/www.youtube.comwatch?v=A0gaXfM1UN0&list=PLQVvvaa0QuDclKx-QpC9wntnURXVJqLyk&index=2。
我想在Mac系统上运行时,更改应用程序名称和标准的文件和帮助菜单。
当我使用这个结构时,它都能正常工作。
if platform == 'darwin':
from Foundation import NSBundle
bundle = NSBundle.mainBundle()
if bundle:
info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
if info and info['CFBundleName'] == 'Python':
info['CFBundleName'] = "LambTracker"
root = tk.Tk()
root.title ("LambTracker")
root.geometry("1200x900")
root.minsize(1000, 750)
mainmenu = tk.Menu(root)
appmenu = tk.Menu(mainmenu, name='apple')
mainmenu.add_cascade(menu=appmenu)
appmenu.add_command(label='About LambTracker', command=aboutlambtracker)
appmenu.add_separator()
root['menu'] = mainmenu
fileMenu = tk.Menu(mainmenu)
mainmenu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Open Database", command=donothing)
fileMenu.add_command(label="New Database", command=donothing)
helpMenu = tk.Menu(mainmenu)
mainmenu.add_cascade(label="Help", menu=helpMenu)
helpMenu.add_command(label="Help", command=donothing)
helpMenu.add_command(label="Release Notes", command=donothing)
helpMenu.add_command(label="GitLab Source Code", command=donothing)
helpMenu.add_command(label="Contact Us", command=donothing)
root.grid()
root.mainloop()
但如果我试图把它放在一个类里面,并把root改为self,我就会得到一个错误的未知选项"-menu",我不明白为什么当目标是root时,它是可用的,但当目标是self时,它就不可用了。
class LambTrackerApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = StartPage(container, self)
self.frames[StartPage] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
mainmenu = tk.Menu(self)
appMenu = tk.Menu(mainmenu, name='apple')
mainmenu.add_cascade(menu=appMenu)
appMenu.add_command(label='About LambTracker', command=aboutlambtracker)
appMenu.add_separator()
self['menu'] = mainmenu
fileMenu = tk.Menu(mainmenu)
mainmenu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Open Database", command=donothing)
fileMenu.add_command(label="New Database", command=donothing)
helpMenu = tk.Menu(mainmenu)
helpMenu.add_command(label="Help", command=donothing)
helpMenu.add_command(label="Release Notes", command= donothing)
helpMenu.add_command(label="GitLab Source Code", command=donothing)
helpMenu.add_command(label="Contact Us", command=donothing)
app = LambTrackerApp()
app.mainloop()
我正在用Python 3.7编写