【问题标题】:Python, Tkinter how to give optionmenu its own windows when pressedPython,Tkinter如何在按下时给optionmenu自己的窗口
【发布时间】:2018-05-04 06:59:54
【问题描述】:

我的计算器上有一个下拉菜单,我将其设置为有 3 个菜单项。 What i want to happen is that when one of the menu items is selected it opens a toplevel window describing that item menu and each one is a different menu because they contain different things how can i code this to happen.我坚持设置它,以便在选择不同的窗口时打开不同的窗口。

def change_dropdown(*args):
    top = Toplevel()
    top.title("READ")
    toplabel = Label(top,text= "Lmao", font = ("Helvetica", 13))
    toplabel.grid()
    button = Button(top, text="Dismiss", relief=FLAT, font = ("Helvetica", 10, "bold"), fg = "ghostwhite", bg = "black", width = "30", height = "2", command=top.destroy)
    button.grid()
    top.focus()


def popmenu():
    global tkvar
    tkvar = StringVar(master)

    choices = {"About","Colour themes", "Contact",}
    popupMenu = OptionMenu(master, tkvar, *choices)
    popupMenu.grid(row = 0, column = 0, columnspan = 5, sticky=W+E+S+N)
    tkvar.set("About") 
    tkvar.trace("w", change_dropdown)

【问题讨论】:

    标签: python tkinter optionmenu


    【解决方案1】:

    据我所知,你想要这样的东西......

    def change_dropdown(*args):
        if tkvar.get() == "About":
            # open About window
        elif tkvar.get() == "Contact":
            # open Contact window
            ...., etc
    

    完整示例

    每个顶级窗口都是一个类似于根窗口的窗口,您可以在其上放置小部件,就像在主窗口上一样。

    从我的角度来看,我稍微更改了代码以使其更具可读性;我从函数中提取了主窗口(主)代码。我将字体规范放在开头以使标签和按钮代码更短。我将字典选项更改为感觉更自然的元组。我给了 OptionMenu 一个宽度,以防止它随着所选的选择而改变大小。

    from tkinter import *
    
    master = Tk()
    master.geometry('300x150+1000+50')
    info = Label(master, text='Press "p" for popup menu')
    info.pack()
    
    
    # Fonts
    H13 = ("Helvetica", 13)
    H10B = ("Helvetica", 10, "bold")
    
    def change_dropdown(*args):
        top = Toplevel()
        if tkvar.get() == "About":      # About window
            top.title("About")
            toplabel = Label(top,text= "The About window", font = H13)
            toplabel.grid()
            button = Button(top, text="Dismiss", relief=FLAT, font = H10B,
                            fg = "ghostwhite", bg = "black", width = "30",
                            height = "2", command=top.destroy)
            button.grid()
            top.focus()
    
        elif tkvar.get() == "Contact":      # Contact window
            top.title("Contact")
            toplabel = Label(top,text= "Contact form", font = H13)
            toplabel.grid()
            button = Button(top, text="Dismiss", relief=FLAT, font = H10B,
                            fg = "ghostwhite", bg = "black", width = "30",
                            height = "2", command=top.destroy)
            button.grid()
            top.focus()
    
        elif tkvar.get() == "Colour themes":    # Color themes window
            top.title("Colour themes")
            toplabel = Label(top,text= "Choose color theme", font = H13)
            toplabel.grid()
            button = Button(top, text="Dismiss", relief=FLAT, font = H10B,
                            fg = "ghostwhite", bg = "black", width = "30",
                            height = "2", command=top.destroy)
            button.grid()
            top.focus()
    
    tkvar = StringVar(master)
    
    def popmenu(event):
        top = Toplevel()
        choices = ("About","Colour themes", "Contact")  # Tuple or List instead of dict
        popupMenu = OptionMenu(top, tkvar, *choices)
        popupMenu.config(width=15)     # Otherwise width varies with option
        popupMenu.grid(row = 0, column = 0, columnspan = 5, sticky=W+E+S+N)
        tkvar.set("Pick one") 
        tkvar.trace("w", change_dropdown)
    
    master.bind('p', popmenu)
    master.mainloop()
    

    这是一个非常简单的界面,但如果您打算让它变得更复杂,我强烈建议您阅读面向对象的 Python。跟踪全局变量很快就会变得很困难。

    【讨论】:

    • 以及如何制作这样的窗口,如为 about 和为联系而制作的。如何编辑这些窗口并在它们上键入文本等,我希望它们是两个不同的窗口,因为联系方式和关于是不同的
    • 已更新示例。
    • 当我运行此代码时,即使我没有选择选项卡,它也会始终打开一个窗口。标签框在我的计算器的顶部,当我启动并且没有晚上在框中选择标签时,它会打开一个窗口我该如何解决这个问题
    • 好吧,你必须从某个地方调用菜单,我认为这不会成为你的障碍。我更改了代码,所以现在我首先创建主窗口并从那里调用弹出菜单。但是你希望它表现如何?你必须考虑一下。也许你想要的是一个常规的弹出菜单。可以使用 Tkinter 菜单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2018-05-26
    • 2011-09-23
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多