python tkinter趣味问答
时间: 2023-11-29 17:47:19 浏览: 114
python--tkinter 的练习题目
5星 · 资源好评率100%
以下是一个简单的Python tkinter趣味问答的例子:
```python
import tkinter as tk
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
question_prompts = [
"What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n",
"What color are bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\n",
"What color are strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n"
]
questions = [
Question(question_prompts[0], "a"),
Question(question_prompts[1], "c"),
Question(question_prompts[2], "b")
]
def check_answer():
global question_number
global score
if user_answer.get().lower() == questions[question_number].answer:
score += 1
question_number += 1
if question_number == len(questions):
question_text.config(text="Quiz completed! Final score: " + str(score))
answer_entry.destroy()
submit_button.destroy()
else:
question_text.config(text=questions[question_number].prompt)
user_answer.set("")
score = 0
question_number = 0
root = tk.Tk()
root.title("Python Quiz")
question_text = tk.Label(root, text=questions[question_number].prompt, font=("Arial", 12))
question_text.pack(pady=10)
user_answer = tk.StringVar()
answer_entry = tk.Entry(root, textvariable=user_answer, font=("Arial", 12))
answer_entry.pack(pady=10)
submit_button = tk.Button(root, text="Submit", command=check_answer)
submit_button.pack(pady=10)
root.mainloop()
```
这个程序会问三个问题,每个问题都有三个选项。用户需要选择正确的选项来回答问题。程序会在用户回答完所有问题后显示用户的得分。
阅读全文