【发布时间】:2019-05-12 21:42:18
【问题描述】:
我有三个文件,我正在尝试通过单选按钮更改变量的值:
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,我似乎无法弄清楚为什么。
【问题讨论】:
-
我有同样的经历,Tkinter 似乎默认使用最后一个单选按钮,或者如果之前没有运行过,则默认使用所有单选按钮。我很想知道是否有人知道以不同方式控制这种情况的方法。
-
@NielsHenkens 我在 'command=' 之后添加了 'lambda:' ,它似乎有效
标签: python tkinter radio-button variable-assignment