一次只允许在 Tkinter 中选择一个单选按钮
Allow only one radiobutton selection in Tkinter at a time
我正在尝试学习 tkinter
模块的基础知识。我制作了这个程序,我有一些问题,每个问题都有一些选项。我的选项使用单选按钮 selection 显示。我想 select 一次独立地为每个问题做一个选择。目前,当我 select 第一个选项时,每个问题的第一个选项都是 selected 但我不希望 selection 除了我正在处理的那个之外。
我的第二个问题是 selection [完成] 后,我想使用 selection 结果并将它们与答案键进行比较,看看有多少答案是正确的。如何存储用户对每个问题的回答?
输出结果:
编辑:
抱歉,我也没有发布我的代码。
这是我正在处理的 python 文件。
from tkinter import *
guessOptions =[]
def display():
global guessOptions
if x.get() == 0:
guessOptions.append("A")
elif x.get() == 1:
guessOptions.append("B")
elif x.get() == 2:
guessOptions.append("C")
else:
guessOptions.append("D")
window = Tk()
answers = ['A', 'B', 'C', 'D']
questions = ["Who invented Bulb? ",
"Which is not passive component? ",
"Which is not related to computer? ",
"Opertor used for and operation? "]
options = [['A. Thomas Edison', 'B. Nikola Tesla', 'C. Albert
Einstien', 'D. Michael Faraday'],
['A. Inductor', 'B. op-amp', 'C. Capacitor', 'D.
Resistor'],
['A. RAM', 'B. SSD', 'C. Heat', 'D. Keyboard'],
['!', '~', '||', '&']]
x = IntVar()
for i in range(len(questions)):
label = Label(window,
text=questions[i],
font=('Arial', 15, 'bold'))
label.pack(anchor=W)
for j in range(len(options)):
checkButton = Radiobutton(window,
text=options[i][j],
variable=x,
value=[j],
padx=10,
font=('Arial', 10),
command=display
)
checkButton.pack(anchor=W)
window.mainloop()
问题的每组答案都需要有自己的 IntVar
,您需要添加 Button
才能触发答案检查过程。我已经在下面的代码中完成了大部分工作,除了 check_answers()
函数并没有真正做任何有意义的事情,因为您没有确切指定将涉及的内容(甚至正确的选择是什么)。
from tkinter import *
guessOptions =[]
def display(x):
global guessOptions
if x.get() == 0:
guessOptions.append("A")
elif x.get() == 1:
guessOptions.append("B")
elif x.get() == 2:
guessOptions.append("C")
else:
guessOptions.append("D")
def check_answers():
print(f'{guessOptions=}')
window = Tk()
answers = ['A', 'B', 'C', 'D']
questions = ["Who invented bulb? ",
"Which is not passive component? ",
"Which is not related to computer? ",
"Operator used for and operation? "]
options = [['A. Thomas Edison', 'B. Nikola Tesla', 'C. Albert Einstein',
'D. Michael Faraday'],
['A. Inductor', 'B. Op-amp', 'C. Capacitor', 'D. Resistor'],
['A. RAM', 'B. SSD', 'C. Heat', 'D. Keyboard'],
['!', '~', '||', '&']]
variables = []
for i in range(len(questions)):
label = Label(window, text=questions[i], font=('Arial', 15, 'bold'))
label.pack(anchor=W)
var = IntVar(value=-1)
variables.append(var) # Save for possible later use - one per question.
def handler(variable=var):
"""Callback for this question and group of answers."""
display(variable)
for j in range(len(options)):
checkButton = Radiobutton(window, text=options[i][j], variable=var,
value=j, padx=10, font=('Arial', 10),
command=handler)
checkButton.pack(anchor=W)
comp_btn = Button(window, text="Check Answers", command=check_answers)
comp_btn.pack()
window.mainloop()
我正在尝试学习 tkinter
模块的基础知识。我制作了这个程序,我有一些问题,每个问题都有一些选项。我的选项使用单选按钮 selection 显示。我想 select 一次独立地为每个问题做一个选择。目前,当我 select 第一个选项时,每个问题的第一个选项都是 selected 但我不希望 selection 除了我正在处理的那个之外。
我的第二个问题是 selection [完成] 后,我想使用 selection 结果并将它们与答案键进行比较,看看有多少答案是正确的。如何存储用户对每个问题的回答?
输出结果:
编辑: 抱歉,我也没有发布我的代码。 这是我正在处理的 python 文件。
from tkinter import *
guessOptions =[]
def display():
global guessOptions
if x.get() == 0:
guessOptions.append("A")
elif x.get() == 1:
guessOptions.append("B")
elif x.get() == 2:
guessOptions.append("C")
else:
guessOptions.append("D")
window = Tk()
answers = ['A', 'B', 'C', 'D']
questions = ["Who invented Bulb? ",
"Which is not passive component? ",
"Which is not related to computer? ",
"Opertor used for and operation? "]
options = [['A. Thomas Edison', 'B. Nikola Tesla', 'C. Albert
Einstien', 'D. Michael Faraday'],
['A. Inductor', 'B. op-amp', 'C. Capacitor', 'D.
Resistor'],
['A. RAM', 'B. SSD', 'C. Heat', 'D. Keyboard'],
['!', '~', '||', '&']]
x = IntVar()
for i in range(len(questions)):
label = Label(window,
text=questions[i],
font=('Arial', 15, 'bold'))
label.pack(anchor=W)
for j in range(len(options)):
checkButton = Radiobutton(window,
text=options[i][j],
variable=x,
value=[j],
padx=10,
font=('Arial', 10),
command=display
)
checkButton.pack(anchor=W)
window.mainloop()
问题的每组答案都需要有自己的 IntVar
,您需要添加 Button
才能触发答案检查过程。我已经在下面的代码中完成了大部分工作,除了 check_answers()
函数并没有真正做任何有意义的事情,因为您没有确切指定将涉及的内容(甚至正确的选择是什么)。
from tkinter import *
guessOptions =[]
def display(x):
global guessOptions
if x.get() == 0:
guessOptions.append("A")
elif x.get() == 1:
guessOptions.append("B")
elif x.get() == 2:
guessOptions.append("C")
else:
guessOptions.append("D")
def check_answers():
print(f'{guessOptions=}')
window = Tk()
answers = ['A', 'B', 'C', 'D']
questions = ["Who invented bulb? ",
"Which is not passive component? ",
"Which is not related to computer? ",
"Operator used for and operation? "]
options = [['A. Thomas Edison', 'B. Nikola Tesla', 'C. Albert Einstein',
'D. Michael Faraday'],
['A. Inductor', 'B. Op-amp', 'C. Capacitor', 'D. Resistor'],
['A. RAM', 'B. SSD', 'C. Heat', 'D. Keyboard'],
['!', '~', '||', '&']]
variables = []
for i in range(len(questions)):
label = Label(window, text=questions[i], font=('Arial', 15, 'bold'))
label.pack(anchor=W)
var = IntVar(value=-1)
variables.append(var) # Save for possible later use - one per question.
def handler(variable=var):
"""Callback for this question and group of answers."""
display(variable)
for j in range(len(options)):
checkButton = Radiobutton(window, text=options[i][j], variable=var,
value=j, padx=10, font=('Arial', 10),
command=handler)
checkButton.pack(anchor=W)
comp_btn = Button(window, text="Check Answers", command=check_answers)
comp_btn.pack()
window.mainloop()