当我运行此代码时:
currentquestion = 0
currentcheckbox = 1
which_radio_var = StringVar(inside_test_root)
while currentquestion < len(questions):
print('currentcheckbox', currentcheckbox)
Radiobutton(inside_test_root, text=questions[currentquestion], value=currentcheckbox, variable=which_radio_var, indicatoron=0, wraplength=30).grid(row=currentquestion+1, column=0)
currentquestion += 2
currentcheckbox += 1
x = which_radio_var.get()
除了让我尝试使用以下代码测试这些单选按钮外,我还可以使单选按钮正常工作,并且一切工作正常:
Button(inside_test_root, text='oof', command=print(x)).grid(column = 77, row = 77)
什么都不会打印。与我制作单选按钮的性质有关吗?我需要一种方法来事先不知道所产生的按钮数量。 任何帮助将不胜感激,谢谢
答案 0 :(得分:0)
就像杰森·哈珀(Jasonharper)所说的那样,我错过了lambda,这意味着我是第一次调用打印函数,而且再也不会调用
“ command = print(x)表示立即打印x,并使用print()函数的返回值(即None)作为单击按钮时要执行的命令。command = lambda:print( which_radio_var.get())可以完成这项工作。“
现在更新的代码为:(变量“ x”已变为“ radio”以适合我的程序的其余部分)
if questiontype == 'Multiple Choice':
currentquestion = 0
currentcheckbox = 1
which_radio_var = StringVar(inside_test_root)
print (which_radio_var)
while currentquestion < len(questions):
print('currentcheckbox', currentcheckbox)
Radiobutton(inside_test_root, text=questions[currentquestion], value=currentcheckbox, variable=which_radio_var, indicatoron=0, wraplength=30).grid(row=currentquestion+1, column=0)
currentquestion += 2
currentcheckbox += 1
print(which_radio_var.get())
radio = which_radio_var.get()
Button(inside_test_root, text='oof', command=lambda: print(radio)).grid(column = 77, row = 77)