Python, Tkinter选项菜单小部件和利用i的制作

2024-09-29 22:20:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一些帧,以便通过使用菜单小部件进行访问。当使用菜单时,你可以点击其中一个命令-它会弹出一个框架,菜单小部件应该仍然在顶部,所以你可以很容易地决定去哪里。在

我试图在一个函数中使用optionmenu小部件,这个函数是在登录页面之后调用的,因此我使用了其中的顶层方法。当我尝试做这个选项菜单时,我遇到了一些问题,我现在被卡住了,也不明白代码有什么问题,所以我希望有人能告诉我它有什么问题。在

CoreContent=命名的函数

myGUI=主根

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")

我很困惑我应该如何以及在哪里创建要添加的框架,作为在选项菜单小部件中使用的命令。在


Tags: 函数命令框架add部件选项菜单label
1条回答
网友
1楼 · 发布于 2024-09-29 22:20:46

有关如何在Tkinter中的帧之间切换的清晰说明,请查看以下链接:Switch between two frames in 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()

相关问题 更多 >

    热门问题