一个简单的Tkinter+SQLite台账管理系统可以有以下功能:
1. 创建数据库表格,包括台账名称、台账编号、物品名称、物品数量、物品价格等字段。
2. 可以添加新的台账信息,包括台账名称、台账编号、物品名称、物品数量、物品价格等信息。
3. 可以编辑、删除已有的台账信息。
4. 可以查询、排序、筛选台账信息。
下面是一个简单的示例代码:
```python
import sqlite3
import tkinter as tk
from tkinter import ttk, messagebox
class AccountBook:
def __init__(self, master):
self.master = master
self.master.title("台账管理系统")
self.master.geometry("800x600")
# 创建数据库表格
self.conn = sqlite3.connect('accountbook.db')
self.cursor = self.conn.cursor()
self.cursor.execute('''CREATE TABLE IF NOT EXISTS accountbook
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
code TEXT NOT NULL,
item TEXT NOT NULL,
quantity INTEGER NOT NULL,
price REAL NOT NULL)''')
# 创建表格控件
columns = ("名称", "编号", "物品", "数量", "价格")
self.tree = ttk.Treeview(self.master, columns=columns, show="headings")
for col in columns:
self.tree.heading(col, text=col)
self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 创建滚动条
scrollbar = ttk.Scrollbar(self.master, orient="vertical", command=self.tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 绑定滚动条与表格
self.tree.config(yscrollcommand=scrollbar.set)
# 添加按钮
add_button = ttk.Button(self.master, text="添加", command=self.add)
add_button.pack(side=tk.TOP, padx=5, pady=5)
edit_button = ttk.Button(self.master, text="编辑", command=self.edit)
edit_button.pack(side=tk.TOP, padx=5, pady=5)
delete_button = ttk.Button(self.master, text="删除", command=self.delete)
delete_button.pack(side=tk.TOP, padx=5, pady=5)
# 加载数据
self.load_data()
def load_data(self):
# 清空表格
for row in self.tree.get_children():
self.tree.delete(row)
# 查询数据
self.cursor.execute("SELECT * FROM accountbook")
rows = self.cursor.fetchall()
# 填充数据到表格
for row in rows:
self.tree.insert("", tk.END, values=row[1:])
def add(self):
# 创建添加对话框
add_dialog = AddDialog(self.master)
if add_dialog.result is not None:
# 插入新数据到数据库中
self.cursor.execute("INSERT INTO accountbook (name, code, item, quantity, price) VALUES (?, ?, ?, ?, ?)", add_dialog.result)
self.conn.commit()
# 刷新表格
self.load_data()
def edit(self):
# 获取选中行数据
selected_row = self.tree.selection()
if len(selected_row) == 0:
messagebox.showerror("错误", "请先选择要编辑的行")
return
data = self.tree.item(selected_row[0])['values']
# 创建编辑对话框
edit_dialog = EditDialog(self.master, data)
if edit_dialog.result is not None:
# 更新数据库中的数据
self.cursor.execute("UPDATE accountbook SET name=?, code=?, item=?, quantity=?, price=? WHERE id=?", (*edit_dialog.result, data[0]))
self.conn.commit()
# 刷新表格
self.load_data()
def delete(self):
# 获取选中行数据
selected_row = self.tree.selection()
if len(selected_row) == 0:
messagebox.showerror("错误", "请先选择要删除的行")
return
# 删除数据库中的数据
data = self.tree.item(selected_row[0])['values']
self.cursor.execute("DELETE FROM accountbook WHERE id=?", (data[0],))
self.conn.commit()
# 刷新表格
self.load_data()
class AddDialog:
def __init__(self, master):
self.result = None
self.top = tk.Toplevel(master)
self.top.title("添加台账信息")
# 创建控件
name_label = ttk.Label(self.top, text="名称:")
name_label.grid(row=0, column=0, padx=5, pady=5)
self.name_entry = ttk.Entry(self.top)
self.name_entry.grid(row=0, column=1, padx=5, pady=5)
code_label = ttk.Label(self.top, text="编号:")
code_label.grid(row=1, column=0, padx=5, pady=5)
self.code_entry = ttk.Entry(self.top)
self.code_entry.grid(row=1, column=1, padx=5, pady=5)
item_label = ttk.Label(self.top, text="物品:")
item_label.grid(row=2, column=0, padx=5, pady=5)
self.item_entry = ttk.Entry(self.top)
self.item_entry.grid(row=2, column=1, padx=5, pady=5)
quantity_label = ttk.Label(self.top, text="数量:")
quantity_label.grid(row=3, column=0, padx=5, pady=5)
self.quantity_entry = ttk.Entry(self.top, validate='key')
self.quantity_entry.grid(row=3, column=1, padx=5, pady=5)
price_label = ttk.Label(self.top, text="价格:")
price_label.grid(row=4, column=0, padx=5, pady=5)
self.price_entry = ttk.Entry(self.top, validate='key')
self.price_entry.grid(row=4, column=1, padx=5, pady=5)
ok_button = ttk.Button(self.top, text="确定", command=self.ok)
ok_button.grid(row=5, column=0, padx=5, pady=5)
cancel_button = ttk.Button(self.top, text="取消", command=self.cancel)
cancel_button.grid(row=5, column=1, padx=5, pady=5)
def ok(self):
# 校验数据
if not self.name_entry.get() or not self.code_entry.get() or not self.item_entry.get() or not self.quantity_entry.get() or not self.price_entry.get():
messagebox.showerror("错误", "请填写完整的信息")
return
try:
quantity = int(self.quantity_entry.get())
price = float(self.price_entry.get())
except ValueError:
messagebox.showerror("错误", "数量和价格必须是数字")
return
# 返回数据
self.result = (self.name_entry.get(), self.code_entry.get(), self.item_entry.get(), quantity, price)
self.top.destroy()
def cancel(self):
self.top.destroy()
class EditDialog:
def __init__(self, master, data):
self.result = None
self.top = tk.Toplevel(master)
self.top.title("编辑台账信息")
# 创建控件
name_label = ttk.Label(self.top, text="名称:")
name_label.grid(row=0, column=0, padx=5, pady=5)
self.name_entry = ttk.Entry(self.top, text=data[0])
self.name_entry.grid(row=0, column=1, padx=5, pady=5)
code_label = ttk.Label(self.top, text="编号:")
code_label.grid(row=1, column=0, padx=5, pady=5)
self.code_entry = ttk.Entry(self.top, text=data[1])
self.code_entry.grid(row=1, column=1, padx=5, pady=5)
item_label = ttk.Label(self.top, text="物品:")
item_label.grid(row=2, column=0, padx=5, pady=5)
self.item_entry = ttk.Entry(self.top, text=data[2])
self.item_entry.grid(row=2, column=1, padx=5, pady=5)
quantity_label = ttk.Label(self.top, text="数量:")
quantity_label.grid(row=3, column=0, padx=5, pady=5)
self.quantity_entry = ttk.Entry(self.top, text=data[3], validate='key')
self.quantity_entry.grid(row=3, column=1, padx=5, pady=5)
price_label = ttk.Label(self.top, text="价格:")
price_label.grid(row=4, column=0, padx=5, pady=5)
self.price_entry = ttk.Entry(self.top, text=data[4], validate='key')
self.price_entry.grid(row=4, column=1, padx=5, pady=5)
ok_button = ttk.Button(self.top, text="确定", command=self.ok)
ok_button.grid(row=5, column=0, padx=5, pady=5)
cancel_button = ttk.Button(self.top, text="取消", command=self.cancel)
cancel_button.grid(row=5, column=1, padx=5, pady=5)
def ok(self):
# 校验数据
if not self.name_entry.get() or not self.code_entry.get() or not self.item_entry.get() or not self.quantity_entry.get() or not self.price_entry.get():
messagebox.showerror("错误", "请填写完整的信息")
return
try:
quantity = int(self.quantity_entry.get())
price = float(self.price_entry.get())
except ValueError:
messagebox.showerror("错误", "数量和价格必须是数字")
return
# 返回数据
self.result = (self.name_entry.get(), self.code_entry.get(), self.item_entry.get(), quantity, price)
self.top.destroy()
def cancel(self):
self.top.destroy()
if __name__ == '__main__':
root = tk.Tk()
accountbook = AccountBook(root)
root.mainloop()
# 关闭数据库连接
accountbook.cursor.close()
accountbook.conn.close()
```
在上面的示例代码中,我们使用Tkinter创建了一个简单的台账管理系统,包括一个表格控件用于展示台账信息,以及添加、编辑、删除按钮用于对台账信息进行管理。同时,我们使用SQLite数据库存储台账信息,通过SQL语句实现数据的增删改查等操作。