python - 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)
解决方案
据我所知,你想要这样的东西......
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。跟踪全局变量很快就会变得很困难。
推荐阅读
- google-my-business-api - 如何使用 Google My Business API 发送回复
- sql-server - 错误 = [Microsoft][SQL Server Native Client 11.0]无法打开 BCP 主机数据文件 SQLState = S1000,NativeError = 0
- cocoapods - Xcode 12(beta 4)在模拟器中构建时无法识别头文件
- ssl - PKI X509:需要 CRL 颁发者详细信息
- python - 相同的代码,但不适用于 jupyter,但适用于 VS
- typescript - 强制元组中的变量与其他变量的泛型类型相同
- python - Python Django 类别表单创建者的问题
- css - 在 CSS3 网格布局期间 grid-row-start 是如何工作的?
- python-3.x - Python3如何从while循环中设置DataFrame
- react-native - 当用户按下回车键或搜索栏或用户停止输入 react native 时我们调用 api 的搜索功能