我有一个CRUD应用程序。使用SQlite3数据库和Python。一切运行良好。绝对没有问题的代码功能如预期。
当我示例化我的python程序的一个***second***示例,并在***first***示例中输入数据时,它不会自动同步或更新第二个示例中的视图。请问我如何让第二个示例同步并刷新显示?
1 -所以我运行我的python脚本,一切都运行良好,我很高兴。
2 -我决定运行我的python脚本的第二个示例,一切都按预期工作。
3 -我在第一个示例中添加了一条记录,一切正常。
4 -我添加的记录在第二个示例中没有立即同步。
5 -如果我关闭第二个示例并重新示例化它,那么数据就会显示出来。
那么,我如何让第二个示例同步数据,而不必关闭和重新示例化,请?数据库源代码如下。
import sqlite3
class Database:
def __init__(self, db):
self.con = sqlite3.connect(db)
self.cur = self.con.cursor()
sql = """
CREATE TABLE IF NOT EXISTS map(
id Integer Primary Key,
name text,
description text,
tod text,
day text,
givenby text
)
"""
self.cur.execute(sql)
self.con.commit()
# Insert Function
def insert(self, name, description, tod, day, givenby):
self.cur.execute("insert into map values (NULL,?,?,?,?,?)",
(name, description, tod, day, givenby))
self.con.commit()
# Fetch All Data from DB
def fetch(self):
self.cur.execute("SELECT * from map")
rows = self.cur.fetchall()
# print(rows)
return rows
# Delete a Record in DB
def remove(self, id):
self.cur.execute("delete from map where id=?", (id,))
self.con.commit()
# Update a Record in DB
def update(self, id, name, description, tod, day, givenby):
self.cur.execute(
"update map set name=?, description=?, tod=?, day=?, givenby=? where id=?",
(name, description, tod, day, givenby, id))
self.con.commit()
显示数据的代码如下所示:
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from db import Database
db = Database("map.db")
from datetime import datetime
print('MAP Tracking System is loading... please wait! This may take a while but still faster than Windows update. ')
root = Tk()
root.title("MAP Tracking & Management System")
root.geometry("1820x920+0+0")
root.config(bg="#2c3e50")
#root.state("zoomed")
name = StringVar()
description = StringVar()
tod = StringVar() #tod = (t)ime (o)f (d)ay
day = StringVar()
givenby = StringVar()
# Entries Frame
entries_frame = Frame(root, bg="#535c68")
entries_frame.pack(side=TOP, fill=X)
title = Label(entries_frame, text="Huntley MAP Tracking & Management System", font=("Calibri", 18, "bold"), bg="#535c68", fg="white")
title.grid(row=0, columnspan=2, padx=10, pady=20, sticky="w")
lblName = Label(entries_frame, text="Name", font=("Calibri", 16), bg="#535c68", fg="white")
lblName.grid(row=1, column=0, padx=10, pady=10, sticky="w")
txtName = Entry(entries_frame, textvariable=name, font=("Calibri", 16), width=30)
txtName.grid(row=1, column=1, padx=10, pady=10, sticky="w")
lblDescription = Label(entries_frame, text="Description", font=("Calibri", 16), bg="#535c68", fg="white")
lblDescription.grid(row=1, column=2, padx=10, pady=10, sticky="w")
txtDescription = Entry(entries_frame, textvariable=description, font=("Calibri", 16), width=30)
txtDescription.grid(row=1, column=3, padx=10, pady=10, sticky="w")
lbldob = Label(entries_frame, text="Time", font=("Calibri", 16), bg="#535c68", fg="white")
lbldob.grid(row=2, column=0, padx=10, pady=10, sticky="w")
txtdob = Entry(entries_frame, textvariable=tod, font=("Calibri", 16), width=30)
txtdob.grid(row=2, column=1, padx=10, pady=10, sticky="w")
lblday = Label(entries_frame, text="Day", font=("Calibri", 16), bg="#535c68", fg="white")
lblday.grid(row=3, column=0, padx=10, pady=10, sticky="w")
txtday = Entry(entries_frame, textvariable=day, font=("Calibri", 16), width=30)
txtday.grid(row=3, column=1, padx=10, pady=10, sticky="w")
lblgivenby = Label(entries_frame, text="Given By", font=("Calibri", 16), bg="#535c68", fg="white")
lblgivenby.grid(row=2, column=2, padx=10, pady=10, sticky="w")
txtgivenby = Entry(entries_frame, textvariable=givenby, font=("Calibri", 16), width=30)
txtgivenby.grid(row=2, column=3, padx=10, pady=10, sticky="w")
tod.set("Auto Captured")
day.set("Auto Captured")
def getData(event):
selected_row = tv.focus()
data = tv.item(selected_row)
global row
row = data["values"]
print("Row Data Below")
print(row)
name.set(row[1])
description.set(row[2])
tod.set(row[3])
day.set(row[4])
givenby.set(row[5])
def dispalyAll():
tv.delete(*tv.get_children())
for row in db.fetch():
tv.insert("", END, values=row)
def add_employee():
if txtName.get() == "" or txtDescription.get() == "" or txtdob.get() == "" or txtday.get() == "" or txtgivenby.get() == "" :
messagebox.showerror("Erorr in Input", "Please Fill All the Details")
return
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
current_day = now.strftime("%A, %d %B %Y") #%A=Weekday %d=date %B=Monthname %Y=Year
#print("Current Time =", current_time)
tod.set("")
tod.set(current_time)
day.set("")
day.set(current_day)
#txtdob.insert(END, current_time)
db.insert(txtName.get(),txtDescription.get(), txtdob.get() ,txtday.get(), txtgivenby.get())
messagebox.showinfo("Success", "Record Inserted")
clearAll()
dispalyAll()
def update_employee():
if txtName.get() == "" or txtDescription.get() == "" or txtdob.get() == "" or txtday.get() == "" or txtgivenby.get() == "" :
messagebox.showerror("Erorr in Input", "Please Fill All the Details")
return
db.update(row[0],txtName.get(), txtDescription.get(), txtdob.get(), txtday.get(), txtgivenby.get())
messagebox.showinfo("Success", "Record Update")
clearAll()
dispalyAll()
def delete_employee():
db.remove(row[0])
clearAll()
dispalyAll()
def clearAll():
name.set("")
description.set("")
tod.set("")
day.set("")
givenby.set("")
btn_frame = Frame(entries_frame, bg="#535c68")
btn_frame.grid(row=6, column=0, columnspan=4, padx=10, pady=10, sticky="w")
btnAdd = Button(btn_frame, command=add_employee, text="Add Details", width=15, font=("Calibri", 16, "bold"), fg="white",
bg="#16a085", bd=0).grid(row=0, column=0)
btnEdit = Button(btn_frame, command=update_employee, text="Update Details", width=15, font=("Calibri", 16, "bold"),
fg="white", bg="#2980b9",
bd=0).grid(row=0, column=1, padx=10)
btnDelete = Button(btn_frame, command=delete_employee, text="Delete Details", width=15, font=("Calibri", 16, "bold"),
fg="white", bg="#c0392b",
bd=0).grid(row=0, column=2, padx=10)
btnClear = Button(btn_frame, command=clearAll, text="Clear Details", width=15, font=("Calibri", 16, "bold"), fg="white",
bg="#f39c12",
bd=0).grid(row=0, column=3, padx=10)
# Table Frame
tree_frame = Frame(root, bg="#ecf0f1")
tree_frame.place(x=0, y=300, width=1980, height=520)
style = ttk.Style()
style.configure("mystyle.Treeview", font=('Calibri', 18),
rowheight=45) # Modify the font of the body
style.configure("mystyle.Treeview.Heading", font=('Calibri', 18)) # Modify the font of the headings
tv = ttk.Treeview(tree_frame, columns=(1, 2, 3, 4, 5, 6, 7, 8), style="mystyle.Treeview")
tv.heading("1", text="ID")
tv.column("1", width=5)
tv.heading("2", text="Name")
tv.heading("3", text="Description")
tv.column("3", width=5)
tv.heading("4", text="Time")
tv.column("4", width=10)
tv.heading("5", text="Day")
tv.column("5", width=35)
tv.heading("6", text="Given By")
tv['show'] = 'headings'
tv.bind("<ButtonRelease-1>", getData)
tv.bind("<Return>", getData)
tv.pack(fill=X)
dispalyAll()
root.mainloop()
1条答案
按热度按时间iovurdzv1#
对于您的情况,可以通过检查数据库文件的修改时间来监视其更新: