我正在创建一个测验(使用tkinter),我想随机洗牌我将显示的问题,但因为它是多项选择题,我必须确保它被正确的选项洗牌,否则,问题和选项就没有意义了
为了实现这一点,我编写了以下代码:
以下是我的选择:
q1 = ['up','in','out']
q2 = ['off', 'out', 'up']
q3 = ['got', 'get', 'down']
q4 = ['out', 'over', 'up']
q5 = ['up', 'under', 'down']
q6 = ['in', 'on', 'over']
q7 = ['down', 'over', 'up']
q8 = ['in', 'over', 'out']
,以及问题:
questions_list = ['Could you possibly put the rubbish ___?',
'Something\'s cropped ___ at work',
'Perhaps we can ___ together then',
'He\'s hanging his jacked ___',
'She\'s tripped him ___',
'He\'s knocked him ___',
'He\'s doing his jacked ___',
'She\'s put her jumper inside ___']
一旦在列表中定义了它们,我就会在字典中使用它们,如图所示:
choices_dic = {'Could you possibly put the rubbish ___?': q1,
'Something\'s cropped ___ at work': q2,
'Perhaps we can ___ together then': q3,
'He\'s hanging his jacked ___': q4,
'She\'s tripped him ___': q5,
'He\'s knocked him ___': q6,
'He\'s doing his jacked ___': q7,
'She\'s put her jumper inside ___': q8}
shuffled = random.sample(questions_list,8)
我用sample
把问题混合起来。为什么?我会添加八个以上的问题,以便在每次重新开始测验时都有不同的问题,或者至少有一些问题是不同的,但现在这并不重要。这就是为什么我在{
window = tk.Tk()
因为我已经定义了列表,所以我将label
这些问题
num1 = tk.IntVar()
num1.set(4) # I copied this eight times, you'll see why soon.
for i in range(1,9):
questions = tk.Label(window, text = shuffled[i - 1]).grid(column = 0, row = (3*i - 1), columnspan = 3, sticky = tk.W)
for x in range(3):
options1 = tk.Radiobutton(window, text = choices_dic[str(shuffled[i-1])], variable = num1, value = x)
options1.grid(column = x, row = 3*i , sticky = tk.W)
options2 = tk.Radiobutton(window, text = choices_dic[str(shuffled[i-1])], variable = num2, value = x)
options2.grid(column = x, row = 3*i, sticky = tk.W)
options3 = tk.Radiobutton(window, text = choices_dic[str(shuffled[i-1])], variable = num3, value = x)
options3.grid(column = x, row = 3*i, sticky = tk.W)
options4 = tk.Radiobutton(window, text = choices_dic[str(shuffled[i-1])], variable = num4, value = x)
options4.grid(column = x, row = 3*i, sticky = tk.W)
options5 = tk.Radiobutton(window, text = choices_dic[str(shuffled[i-1])], variable = num5, value = x)
options5.grid(column = x, row = 3*i, sticky = tk.W)
options6 = tk.Radiobutton(window, text = choices_dic[str(shuffled[i-1])], variable = num6, value = x)
options6.grid(column = x, row = 3*i, sticky = tk.W)
options7 = tk.Radiobutton(window, text = choices_dic[str(shuffled[i-1])], variable = num7, value = x)
options7.grid(column = x, row = 3*i, sticky = tk.W)
options8 = tk.Radiobutton(window, text = choices_dic[str(shuffled[i-1])], variable = num8, value = x)
options8.grid(column = x, row = 3*i, sticky = tk.W)
window.mainloop()
这是混乱的,当然是错误的。在我们到达那里之前,我想澄清一下choices_dic[str(shuffled[i-1])]
的作用。基本上是按照给定的顺序从choices_dic
字典中获取一个元素(检查范围),因为它必须是字符串,所以我使用了str()
方法
上面的代码是错误的,因为它执行以下操作:
如您所见,当您选择一个单选按钮时,您将选择其行中的所有单选按钮。您还可以注意到,对于每行中的每个单选按钮,都有一个标记为choices_dic[str(shuffled[i-1])]
的列表。不是我想要的
因此,事实上,我还有另一个问题。如何在所有行中显示每个radiobutton的choices_dic
中的每个元素,也就是说,不显示每个radiobutton的整个列表三次(如上图所示)
我知道是什么导致了这个问题,但我不知道如何解决它
我希望这足以让你明白我做了什么
编辑: 有一段时间了,我差点忘了我问过这个问题。我的解决办法是: 这很简单。通过上面的步骤,您可以创建一个类或函数来执行代码
目前没有回答
相关问题 更多 >
编程相关推荐