首页 > 解决方案 > Python,Tkinter如何在按下时给optionmenu自己的窗口

问题描述

我的计算器上有一个下拉菜单,我将其设置为有 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)

标签: pythontkinteroptionmenu

解决方案


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

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。跟踪全局变量很快就会变得很困难。


推荐阅读