【发布时间】:2014-08-28 21:37:18
【问题描述】:
我想使用 tkinter 创建一个自我报告问卷。此问卷有多个问题,用户应使用 0 到 4 范围内的数值回答每个问题(其中“0”代表“绝对不是”,“4”代表“绝对是”)
我使用标签来打包问题和单选按钮以供用户响应。
我想要做的是获取每个问题,首先是特定问题的索引,然后是用户选择的相对响应。这是我创建响应单选按钮时的代码部分:
class Questionnaire:
...
# response alternatives (from 0 to 4)
def add_resps(self):
self.question_index = {}
self.var_list = []
for i in range(len(self.affs)): # "self.affs" is the list of questions
self.question_index[i] = i
var = IntVar()
self.var_list.append(var)
for r in range(len( self.resps )):
col_Resp = 5 # previous columns are occupied by questions
self.wNumResp = Radiobutton(self.affs_frame,
text=r,
variable= self.var_list[i],
value=r,
command= lambda: self.get_resp(
self.question_index[i],
self.var_list[i]
),
bg="white",
fg="black",
font='Arial 10 bold',
relief=SOLID)
self.wNumResp.grid(row=i, column=r+colRisp, sticky=N+E+S+W)
def get_resp(self, question, response ):
print 'question n.', question, 'user\'s response:', response.get()
但是...当我通过单击单选按钮测试代码是否有效时,无论我在回答什么问题时选择的单选按钮,我总是得到相同的输出:
>>>
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
有人可以帮帮我吗?
提前致谢
【问题讨论】:
标签: python tkinter radio-button