Python tkinter+sqlit3智力问答设计
时间: 2024-01-12 19:05:34 浏览: 166
好的,下面是一个简单的示例代码,实现了基于Python tkinter和sqlite3的智力问答设计。
```python
import tkinter as tk
import sqlite3
# 创建数据库连接
conn = sqlite3.connect('quiz.db')
c = conn.cursor()
# 创建问题表
c.execute('''CREATE TABLE IF NOT EXISTS questions
(id INTEGER PRIMARY KEY AUTOINCREMENT,
question TEXT,
answer TEXT)''')
# 插入问题数据
c.execute("INSERT INTO questions (question, answer) VALUES (?, ?)",
('什么是Python?', '一种高级编程语言'))
c.execute("INSERT INTO questions (question, answer) VALUES (?, ?)",
('Python的创始人是谁?', 'Guido van Rossum'))
c.execute("INSERT INTO questions (question, answer) VALUES (?, ?)",
('Python中如何定义一个函数?', 'def function_name(parameters):'))
# 提交更改并关闭数据库连接
conn.commit()
conn.close()
# 创建窗口
root = tk.Tk()
root.title('智力问答')
# 创建问题标签
question_label = tk.Label(root, text='问题:', font=('Arial', 16))
question_label.pack(pady=10)
# 创建问题文本框
question_text = tk.Text(root, font=('Arial', 14), width=40, height=5)
question_text.pack()
# 创建答案标签
answer_label = tk.Label(root, text='答案:', font=('Arial', 16))
answer_label.pack(pady=10)
# 创建答案文本框
answer_text = tk.Text(root, font=('Arial', 14), width=40, height=5)
answer_text.pack()
# 创建下一题按钮
def next_question():
# 获取下一道问题
conn = sqlite3.connect('quiz.db')
c = conn.cursor()
c.execute('SELECT * FROM questions ORDER BY RANDOM() LIMIT 1')
row = c.fetchone()
conn.close()
# 显示问题和清空答案
question_text.delete(1.0, tk.END)
question_text.insert(tk.END, row[1])
answer_text.delete(1.0, tk.END)
next_button = tk.Button(root, text='下一题', font=('Arial', 14), command=next_question)
next_button.pack(pady=10)
# 创建检查答案按钮
def check_answer():
# 获取当前问题的答案
conn = sqlite3.connect('quiz.db')
c = conn.cursor()
c.execute("SELECT answer FROM questions WHERE question=?", (question_text.get(1.0, tk.END).strip(),))
row = c.fetchone()
conn.close()
# 检查答案并显示结果
if row:
if answer_text.get(1.0, tk.END).strip() == row[0]:
tk.messagebox.showinfo('结果', '答案正确!')
else:
tk.messagebox.showerror('结果', '答案错误!')
else:
tk.messagebox.showerror('错误', '未找到该问题的答案!')
check_button = tk.Button(root, text='检查答案', font=('Arial', 14), command=check_answer)
check_button.pack(pady=10)
# 显示第一道问题
next_question()
# 运行窗口
root.mainloop()
```
在这个示例代码中,我们首先创建了一个sqlite3数据库,并在其中创建了一个问题表,并插入了一些问题数据。
然后,我们使用Python tkinter创建了一个窗口,包含问题文本框、答案文本框、下一题按钮和检查答案按钮。当点击下一题按钮时,窗口将显示一个随机的问题,并清空答案文本框。当点击检查答案按钮时,窗口将检查当前问题的答案,并显示相应的结果。
需要注意的是,这只是一个基本示例代码,您可以根据自己的需求进行修改和扩展。例如,您可以添加更多的问题和答案,或者改变问题和答案的格式。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)