Python,Tkinter如何在按下时为optionmenu提供自己的窗口

时间:2018-05-04 06:59:54

标签: python tkinter optionmenu

我的计算器上有一个下拉菜单,我已将其设置为有3个菜单项。我想要发生的是,当选择其中一个菜单项时,它会打开一个描述该项目菜单的顶层窗口,每个菜单都是一个不同的菜单,因为它们包含不同的内容,我怎么能对此进行编码。我坚持设置它,以便在选择不同的窗口时打开不同的窗口。

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)

1 个答案:

答案 0 :(得分:0)

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

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

完整示例

每个Toplevel窗口都是一个像根窗口一样的窗口,你就像在master上一样把小部件放在它上面。

我改变了一些代码,以便从我的角度来看它更具可读性;我从函数中提取了主窗口(主)代码。我把字体规格放在开头,使标签和按钮代码更短。我把Dict的选择改成了一个感觉更自然的元组。我为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。跟踪全局变量很快就会变得困难。