gpt4 book ai didi

python - 链接两个 OptionMenu 小部件 Tkinter

转载 作者:太空宇宙 更新时间:2023-11-03 19:16:46 24 4
gpt4 key购买 nike

我有两个 OptionMenu 小部件,如下所示的简单代码片段:

    variable = StringVar(win1)                               
variable.set(number(number2))
type = OptionMenu(win1, variable, "None", "Clear", "Dark", "Heavy", )
type.grid(row=i, column=3, sticky="nsew", padx=1, pady=1)


variableunit = StringVar(win1)
variableunit.set(unit)
unit = OptionMenu(win1, variableunit, "colour", "shade")
unit.grid(row=i, column=5, sticky="nsew", padx=1, pady=1)

我尝试过使用回调函数进行跟踪,但到目前为止还没有成功。我想在第一个菜单中选择“重”时链接,第二个菜单始终为“颜色”。对于其余的选择,第二个菜单必须始终默认为“shade”,但可以更改。

如果有人能帮助我,我将不胜感激。我已经查看了 effbot 网站的变量和痕迹,但仍然卡住了。

最佳答案

目前还不清楚你想要什么,但我认为这应该可以做到。

当在第一个菜单中选择“重”时,在第二个菜单中选择“颜色”,并且该菜单被禁用(无法选择其他任何内容)。当在第一个菜单中选择其他内容时,第二个菜单将转到“mm”并再次启用。

from Tkinter import *

class app:
def __init__(self, root):
win1 = Frame(root)
win1.grid(row=0,column=0)

self.variable = StringVar(win1)
self.variable.set(42)
self.type = OptionMenu(win1, self.variable,
"None", "Clear", "Dark", "Heavy",
command = self.varMenu)
self.type.grid(row=1, column=3, sticky="nsew", padx=1, pady=1)


self.variableunit = StringVar(win1)
self.variableunit.set('mm')
self.unit = OptionMenu(win1,
self.variableunit, "mm", "colour", "shade")
self.unit.grid(row=1, column=5, sticky="nsew", padx=1, pady=1)

def varMenu(self, selection):
if selection == "Heavy":
self.variableunit.set("colour")
self.unit.config(state = DISABLED)
else:
self.variableunit.set("mm")
self.unit.config(state = NORMAL)

root = Tk()
a = app(root)
root.mainloop()

关于python - 链接两个 OptionMenu 小部件 Tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11097994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com