我有三个文件,我正在尝试通过单选按钮更改变量的值:
Config.py
epoch=1
imageClassifier.py
import config
train(args, model, device, train_loader, optimizer, config.epoch)
GUI.py
import config
def changeEpoch(epochValue):
config.epoch=epochValue
var1 = IntVar()
epochRadioButton1 = Radiobutton(middleFrame, variable=var1, value=1,
text="1", command=changeEpoch(1))
epochRadioButton5 = Radiobutton(middleFrame, variable=var1, value=2,
text="5", command=changeEpoch(5))
epochRadioButton10 = Radiobutton(middleFrame, variable=var1, value=3,
text="10", command=changeEpoch(10))
epochRadioButton20 = Radiobutton(middleFrame, variable=var1, value=4,
text="20", command=changeEpoch(20))
var1.set(1)
但是,无论如何,当我运行程序时,epoch的值始终为20,而我似乎无法找出原因。
答案 0 :(得分:0)
考虑以下代码行:
python -m label_image \
--graph=tf_files/retrained_graph.pb \
--image=tf_files/image/bomi/bomi_0001.jpg
它具有与此完全相同的效果:
epochRadioButton20 = Radiobutton(..., command=changeEpoch(20))
result = changeEpoch(20)
epochRadioButton20 = Radiobutton(..., command=result)
属性带有一个可调用。但是,您的代码会立即调用该函数并将结果赋予command
属性。
我建议您不要让新值传递给函数,而是让函数从单选按钮中获取值。为此,请进行以下更改:
command
另一种解决方案是使用def changeEpoch():
epochValue = var1.get()
config.epoch=epochValue
...
epochRadioButton20 = Radiobutton(..., command=lambda: changeEpoch)
创建一个调用lambda
的匿名函数(假定您不修改changeEpoch
):
changeEpoch