python实现多选题答题tkinter
时间: 2023-12-03 11:40:03 浏览: 255
Python实现定时自动关闭的tkinter窗口方法
以下是一个简单的Python实现多选题答题的Tkinter例子:
```python
import tkinter as tk
class MultiChoiceQuestion:
def __init__(self, master, question, choices):
self.master = master
self.question = question
self.choices = choices
self.answers = []
self.var_list = []
self.create_widgets()
def create_widgets(self):
# 显示问题
tk.Label(self.master, text=self.question).pack()
# 显示选项
for choice in self.choices:
var = tk.BooleanVar()
self.var_list.append(var)
tk.Checkbutton(self.master, text=choice, variable=var).pack()
# 显示提交按钮
tk.Button(self.master, text="提交", command=self.submit).pack()
def submit(self):
# 获取答案
for i in range(len(self.choices)):
if self.var_list[i].get():
self.answers.append(self.choices[i])
# 显示答案
tk.Label(self.master, text="你的答案是:" + ", ".join(self.answers)).pack()
root = tk.Tk()
question = "以下哪些是编程语言?"
choices = ["Python", "HTML", "CSS", "Java", "JavaScript"]
MultiChoiceQuestion(root, question, choices)
root.mainloop()
```
运行上述代码后,会弹出一个窗口,显示一个多选题,用户可以选择一个或多个选项,然后点击提交按钮,程序会显示用户选择的答案。
阅读全文