python tkinter sqlite3
时间: 2023-04-25 18:00:11 浏览: 55
Python tkinter 是 Python 的一个 GUI 库,可以用来创建图形用户界面。而 SQLite3 则是 Python 中的一个轻量级数据库,可以用来存储和管理数据。两者结合使用可以创建一个具有图形界面的数据库应用程序。
相关问题
使用python和SQLite3数据库
好的,以下是一个基于Python和SQLite3数据库的简单界面设计方案:
```python
import sqlite3
import tkinter as tk
# 创建数据库连接
conn = sqlite3.connect('cal.db')
c = conn.cursor()
# 创建计算记录表
c.execute('''CREATE TABLE IF NOT EXISTS cal_records
(id INTEGER PRIMARY KEY AUTOINCREMENT,
num1 REAL,
num2 REAL,
op TEXT,
result REAL)''')
# 定义计算函数
def calculate():
num1 = float(entry1.get())
num2 = float(entry2.get())
op = listbox.get(listbox.curselection())
if op == '+':
result = num1 + num2
elif op == '-':
result = num1 - num2
elif op == '*':
result = num1 * num2
elif op == '/':
result = num1 / num2
else:
result = 0.0
result_label.config(text=str(result))
# 存储计算结果到数据库
c.execute('INSERT INTO cal_records (num1, num2, op, result) VALUES (?, ?, ?, ?)',
(num1, num2, op, result))
conn.commit()
# 定义查询函数
def query():
num1 = float(entry1.get())
c.execute('SELECT * FROM cal_records WHERE num1=?', (num1,))
records = c.fetchall()
result_str = ''
for record in records:
result_str += f'num1: {record[1]}, num2: {record[2]}, op: {record[3]}, result: {record[4]}\n'
result_label.config(text=result_str)
# 创建界面
root = tk.Tk()
root.title('Calculator')
label1 = tk.Label(root, text='Number 1:')
label1.grid(row=0, column=0)
entry1 = tk.Entry(root)
entry1.grid(row=0, column=1)
label2 = tk.Label(root, text='Number 2:')
label2.grid(row=1, column=0)
entry2 = tk.Entry(root)
entry2.grid(row=1, column=1)
listbox = tk.Listbox(root)
listbox.grid(row=2, column=0, columnspan=2)
listbox.insert(0, '+', '-', '*', '/')
calculate_btn = tk.Button(root, text='Calculate', command=calculate)
calculate_btn.grid(row=3, column=0)
query_btn = tk.Button(root, text='Query', command=query)
query_btn.grid(row=3, column=1)
result_label = tk.Label(root, text='')
result_label.grid(row=4, column=0, columnspan=2)
root.mainloop()
# 关闭数据库连接
c.close()
conn.close()
```
在上述界面中,我们使用了Python的Tkinter库来创建GUI界面。界面中包含两个文本框用于输入两个数值,一个列表框用于选择运算方法,一个计算按钮和一个查询按钮。在点击计算按钮时,程序会根据用户的输入和选择进行相应的数学运算,并将结果显示在界面上。同时,程序会将计算结果存入SQLite3数据库中。在点击查询按钮时,程序会根据用户输入的第一个数值从数据库中查询相应的计算结果,并将结果显示在界面上。
需要注意的是,在使用SQLite3数据库时,我们需要首先建立数据库连接,然后再通过cursor对象执行SQL语句进行数据库操作。最后,务必在程序结束时关闭数据库连接。
Python tkinter+sqlit3智力问答设计
好的,下面是一个简单的示例代码,实现了基于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创建了一个窗口,包含问题文本框、答案文本框、下一题按钮和检查答案按钮。当点击下一题按钮时,窗口将显示一个随机的问题,并清空答案文本框。当点击检查答案按钮时,窗口将检查当前问题的答案,并显示相应的结果。
需要注意的是,这只是一个基本示例代码,您可以根据自己的需求进行修改和扩展。例如,您可以添加更多的问题和答案,或者改变问题和答案的格式。