如果你能理解,请用python写一个完整代码
时间: 2024-01-21 19:02:21 浏览: 137
好的,以下是一个简单的Python代码示例,包含了您需要的功能。由于您并没有说明您想要使用哪种数据库,我使用了Python内置的SQLite数据库。如果您要使用其他数据库,需要按照对应的语法进行修改。
```python
import sqlite3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from datetime import datetime
# 创建数据库连接
conn = sqlite3.connect('data.db')
cursor = conn.cursor()
# 创建数据表
cursor.execute('''CREATE TABLE IF NOT EXISTS data
(id INTEGER PRIMARY KEY AUTOINCREMENT,
line TEXT,
robot TEXT,
model TEXT,
current_point INTEGER,
total_point INTEGER,
record_time TEXT,
record_filename TEXT)''')
conn.commit()
# 创建GUI窗口
root = tk.Tk()
root.title('数据查询工具')
# 定义查询函数
def search_data():
line = line_var.get()
robot = robot_var.get()
model = model_var.get()
record_time = record_time_var.get()
cursor.execute('''SELECT * FROM data WHERE line=? AND robot=? AND model=? AND record_time=?''',
(line, robot, model, record_time))
data = cursor.fetchall()
if len(data) == 0:
messagebox.showinfo('提示', '没有找到匹配的数据')
else:
for i in treeview.get_children():
treeview.delete(i)
for row in data:
treeview.insert('', 'end', values=row[1:])
# 定义对比函数
def compare_data():
line = line_var.get()
robot = robot_var.get()
model = model_var.get()
cursor.execute('''SELECT * FROM data WHERE line=? AND robot=? AND model=? ORDER BY record_time ASC''',
(line, robot, model))
data = cursor.fetchall()
if len(data) == 0:
messagebox.showinfo('提示', '没有找到匹配的数据')
elif len(data) == 1:
messagebox.showinfo('提示', '只有一条数据,无法进行对比')
else:
data1 = data[0]
data2 = data[-1]
compare_str = ''
for i in range(1, 6):
if data1[i] != data2[i]:
compare_str += f'{data1[i]} -> {data2[i]} '
messagebox.showinfo('对比结果', compare_str)
# 定义添加数据函数
def add_data():
line = line_var.get()
robot = robot_var.get()
model = model_var.get()
current_point = current_point_var.get()
total_point = total_point_var.get()
record_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
record_filename = record_filename_var.get()
cursor.execute('''INSERT INTO data(line, robot, model, current_point, total_point, record_time, record_filename)
VALUES (?, ?, ?, ?, ?, ?, ?)''',
(line, robot, model, current_point, total_point, record_time, record_filename))
conn.commit()
messagebox.showinfo('提示', '数据已添加')
line_var.set('')
robot_var.set('')
model_var.set('')
current_point_var.set('')
total_point_var.set('')
record_filename_var.set('')
search_data()
# 定义清空数据函数
def clear_data():
cursor.execute('''DELETE FROM data''')
conn.commit()
for i in treeview.get_children():
treeview.delete(i)
messagebox.showinfo('提示', '所有数据已清空')
# 创建GUI控件
line_label = ttk.Label(root, text='线体编号')
line_var = tk.StringVar()
line_entry = ttk.Entry(root, textvariable=line_var)
robot_label = ttk.Label(root, text='机器人编号')
robot_var = tk.StringVar()
robot_entry = ttk.Entry(root, textvariable=robot_var)
model_label = ttk.Label(root, text='机种编号')
model_var = tk.StringVar()
model_entry = ttk.Entry(root, textvariable=model_var)
record_time_label = ttk.Label(root, text='记录时间')
record_time_var = tk.StringVar()
record_time_entry = ttk.Entry(root, textvariable=record_time_var)
search_button = ttk.Button(root, text='查询', command=search_data)
compare_button = ttk.Button(root, text='对比', command=compare_data)
current_point_label = ttk.Label(root, text='当前第几点')
current_point_var = tk.StringVar()
current_point_entry = ttk.Entry(root, textvariable=current_point_var)
total_point_label = ttk.Label(root, text='总共几点')
total_point_var = tk.StringVar()
total_point_entry = ttk.Entry(root, textvariable=total_point_var)
record_filename_label = ttk.Label(root, text='记录文件名')
record_filename_var = tk.StringVar()
record_filename_entry = ttk.Entry(root, textvariable=record_filename_var)
add_button = ttk.Button(root, text='添加数据', command=add_data)
clear_button = ttk.Button(root, text='清空数据', command=clear_data)
treeview = ttk.Treeview(root, columns=('line', 'robot', 'model', 'current_point', 'total_point', 'record_time', 'record_filename'))
treeview.heading('line', text='线体编号')
treeview.heading('robot', text='机器人编号')
treeview.heading('model', text='机种编号')
treeview.heading('current_point', text='当前第几点')
treeview.heading('total_point', text='总共几点')
treeview.heading('record_time', text='记录时间')
treeview.heading('record_filename', text='记录文件名')
# 布局GUI控件
line_label.grid(row=0, column=0, padx=5, pady=5)
line_entry.grid(row=0, column=1, padx=5, pady=5)
robot_label.grid(row=0, column=2, padx=5, pady=5)
robot_entry.grid(row=0, column=3, padx=5, pady=5)
model_label.grid(row=0, column=4, padx=5, pady=5)
model_entry.grid(row=0, column=5, padx=5, pady=5)
record_time_label.grid(row=0, column=6, padx=5, pady=5)
record_time_entry.grid(row=0, column=7, padx=5, pady=5)
search_button.grid(row=0, column=8, padx=5, pady=5)
compare_button.grid(row=0, column=9, padx=5, pady=5)
current_point_label.grid(row=1, column=0, padx=5, pady=5)
current_point_entry.grid(row=1, column=1, padx=5, pady=5)
total_point_label.grid(row=1, column=2, padx=5, pady=5)
total_point_entry.grid(row=1, column=3, padx=5, pady=5)
record_filename_label.grid(row=1, column=4, padx=5, pady=5)
record_filename_entry.grid(row=1, column=5, padx=5, pady=5)
add_button.grid(row=1, column=6, padx=5, pady=5)
clear_button.grid(row=1, column=7, padx=5, pady=5)
treeview.grid(row=2, column=0, columnspan=10, padx=5, pady=5)
# 运行GUI程序
root.mainloop()
```
这段代码使用了Tkinter库来创建GUI窗口,使用了SQLite数据库来存储和管理数据。您可以根据自己的需求进行修改,例如更改GUI控件的样式、更改数据表结构、更改查询和对比方法等等。
阅读全文