3 回答
TA贡献1860条经验 获得超9个赞
将您的功能更改为:
def changeState():
pick = choose.get()
if (pick == "op2"):
button['state'] = ACTIVE #means active state
button.config(text = "ACTIVE")
else:
button['state'] = DISABLED #means disabled state
button.config(text = "Disabled")
另外,您没有调用您的函数,因此要调用它并使效果处于活动状态,请在选项菜单中添加命令参数,例如:
options = OptionMenu(app, choose, "op1", "op2",command=lambda _:changeState())
使用lambda _:optionmenu 命令需要传递一个 tkinter 变量,以避免这种情况。您还可以为您的函数提供一个参数,但是如果您在其他地方调用您的函数,您将必须传递一个参数,或者您也可以使用类似的参数并point=None摆脱lambda.
希望这可以清除错误,如果有任何疑问,请告诉我。
TA贡献1817条经验 获得超6个赞
您可以在更改时command=changeState添加OptionMenu(...)调用:changeState()options
def changeState(pick):
if pick == "op2":
button['state'] = ACTIVE
button.config(text = "ACTIVE")
else:
button['state'] = DISABLED
button.config(text = "Disabled")
...
options = OptionMenu(app, choose, "op1", "op2", command=changeState)
TA贡献1789条经验 获得超10个赞
知道了!
编辑:我稍微改变了第9行,删除了button.active并替换为NORMAL,这有效。
from tkinter import *
def setLabel():
changed.set("Active")
def changeState(*args):
pick = choose.get()
if (pick == "op2"):
button['state'] = NORMAL
button.config(text = "ACTIVE")
else:
button['state'] = DISABLED
button.config(text = "Disabled")
app = Tk()
app.resizable(40,40)
choose = StringVar()
choose.set("op1")
options = OptionMenu(app, choose, "op1", "op2")
choose.trace("w", changeState)
options.pack()
button = Button(app, text = "Disabled", state = DISABLED, command = setLabel)
button.pack()
changed = StringVar()
label = Label(app, textvariable = changed, font = ("helvetica", 10))
label.pack()
app.mainloop()
添加回答
举报