Python、Tkinter OptionMenu小部件&;利用它
我试图通过使用菜单小部件创建一些要访问的帧。当使用菜单时,你可以点击其中一个命令——它会弹出一个框架,菜单小部件应该仍然在顶部,这样你就可以很容易地决定去哪里 我试图在登录页面后调用的函数中使用option menu小部件,因此我在其中使用顶级方法。在尝试使用这个选项菜单时,我遇到了一些问题,目前我无法理解代码的错误,所以我希望有人能告诉我代码的错误 CoreContent=名为 myGUI=主根Python、Tkinter OptionMenu小部件&;利用它,python,tkinter,Python,Tkinter,我试图通过使用菜单小部件创建一些要访问的帧。当使用菜单时,你可以点击其中一个命令——它会弹出一个框架,菜单小部件应该仍然在顶部,这样你就可以很容易地决定去哪里 我试图在登录页面后调用的函数中使用option menu小部件,因此我在其中使用顶级方法。在尝试使用这个选项菜单时,我遇到了一些问题,目前我无法理解代码的错误,所以我希望有人能告诉我代码的错误 CoreContent=名为 myGUI=主根 def CoreContent(): #Building core content/struct
def CoreContent():
#Building core content/structure
myGUI.withdraw() # This is the main root that I remove after user logs in
CoreRoot = Toplevel(myGUI, bg="powderblue") # Toplevel
CoreRoot.title("titletest")
CoreRoot.geometry('300x500')
CoreRoot.resizable(width=False, height=False)
#Creating drop-down menu
menu = Menu(CoreRoot)
CoreRoot.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="test one", command=lambda: doNothing()) # Problem
filemenu.add_command(label="soon")
filemenu.add_separator()
filemenu.add_command(label="Exit")
我不知道应该如何以及在何处创建要添加的帧,作为在选项菜单小部件中使用的命令 有关如何在Tkinter中切换帧的清晰说明,请查看以下链接: 要从菜单中执行此操作,您可以编写如下内容:
import tkinter as tk
# method to raise a frame to the top
def raise_frame(frame):
frame.tkraise()
# Create a root, and add a menu
root = tk.Tk()
menu = tk.Menu(root)
root.config(menu=menu)
filemenu = tk.Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="test one", command=lambda: raise_frame(f1))
filemenu.add_command(label="test two", command=lambda: raise_frame(f2))
# Create two frames on top of each other
f1 = tk.Frame(root)
f2 = tk.Frame(root)
for frame in (f1, f2):
frame.grid(row=0, column=0, sticky='news')
# Add widgets to the frames
tk.Label(f1, text='FRAME 1').pack()
tk.Label(f2, text='FRAME 2').pack()
# Launch the app
root.mainloop()