我做了一些研究,发现要在python中改变一个方法中的全局变量,你必须通过card_1BLoXK2eZvKYlo2CUZoDPXh1
,然后继续用方法改变它。我试图根据tkinter Optionmenu选择将变量更改为true,但无济于事。我做错了什么?
可验证的例子:
global variablename
答案 0 :(得分:1)
主要问题来自FilterChoiceVar.get() is "All"
表达式,它永远不会是真的。一个好的做法是始终使用'=='而不是'is'来比较字符串。这是我修改过的代码,包括一些代码清理:
from tkinter import *
AllCheck = False
filterList = ["All","Not All"]
def check_dropdown(*args):
global AllCheck
AllCheck = FilterChoiceVar.get() == "All"
def scanBus():
check_dropdown()
if ScanVar.get() and AllCheck:
print("AllCheck in action!")
GuiWindow = Tk()
FilterChoiceVar = StringVar(GuiWindow)
FilterChoiceVar.set("Not All")
FilterChoice = OptionMenu(GuiWindow, FilterChoiceVar, *filterList)
FilterChoice.grid(row=0, column=0)
ScanVar = BooleanVar()
ScanButton = Checkbutton(GuiWindow, text="scan", variable=ScanVar,
command=scanBus, indicatoron=0)
ScanButton.grid(row=1, column=0)
GuiWindow.geometry('{}x{}'.format(100, 60))
GuiWindow.mainloop()