我目前遇到的问题是,当我添加新班次时,我无法让GUI正常更新。目前,我似乎得到的最好的是获取更新的信息,但它显示在旧信息之上。
我确实阅读了整个mainloop()
问题,并尝试使用after()
方法,但我不确定这是不是我要找的:
class ViewShifts(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
buttonFrame = tk.Frame(self)
buttonFrame.pack(side='bottom', fill='both', expand=True)
#frame to display shifts
table = tk.Frame(self)
table.pack(side='top', fill='both', expand=True)
table.destroy()
#dictionary which will contain widgets that will display shifts (in viewNew method)
self.widgets = {}
button1= tk.Button(self, text="Home",
command=lambda: controller.showFrame(StartPage))
button1.pack(padx=0, pady=10, side='bottom')
button2 = tk.Button(self, text='Update',
command=lambda: deleteShiftFrame(self))
button2.pack(padx=0, pady=10, side='bottom')
text = tk.Label(self, text="All shifts", font=LARGEFONT)
text.pack(padx=10, side='top')
def deleteShiftFrame(self):
table.destroy()
viewNew(self)
def viewNew(self):
#frame to display shifts
table = tk.Frame(self)
table.pack(side='top', fill='both', expand=True)
row = 0
#make dictionary empty
self.widgets = {}
c.execute("SELECT * FROM shifts")
data = c.fetchall()
#create labels for each column
date_label = tk.Label(table, text="Date")
shift_label=tk.Label(table, text="Shift")
shop_label=tk.Label(table, text="Shop")
hours_label=tk.Label(table, text="Hours")
#add labels to grid
date_label.grid(row=0, column=0, sticky="nsw")
shift_label.grid(row=0, column=1, sticky="nsw")
shop_label.grid(row=0, column=2, sticky="nsw")
hours_label.grid(row=0, column=3, sticky="nsw")
#for each column create a tk label for each row within column with corresponding details
for id, date, shift, shop, hours in (data):
row+=1
self.widgets[id] = {
"id":tk.Label(table, text=id),
"date":tk.Label(table, text=date),
"shift":tk.Label(table, text=shift),
"shop":tk.Label(table, text=shop),
"hours":tk.Label(table, text=hours)
}
#add current row of column to grid
self.widgets[id]["date"].grid(row=row, column=0, sticky="nsw")
self.widgets[id]["shift"].grid(row=row, column=1, sticky="nsw")
self.widgets[id]["shop"].grid(row=row, column=2, sticky="nsw")
self.widgets[id]["hours"].grid(row=row, column=3, sticky="nsw")
#add blank column between each field for spacing
table.grid_columnconfigure(0, weight=1)
table.grid_columnconfigure(1, weight=1)
table.grid_columnconfigure(2, weight=1)
table.grid_columnconfigure(3, weight=1)
#add extra row for padding
table.grid_rowconfigure(row+1, weight=1)
viewNew(self)
class AddShifts(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
text = tk.Label(self, text = "Add shifts", font=LARGEFONT)
text.pack(padx=10, side = 'top')
#create frame for entry
AddShiftFrame = tk.Frame(self)
AddShiftFrame.pack(side="top", fill="both", expand=True)
#add column headers
dateLabel = tk.Label(AddShiftFrame, text="Date")
shiftLabel = tk.Label(AddShiftFrame, text="Shift")
shopLabel = tk.Label(AddShiftFrame, text='Shop')
dateLabel.grid(row=1, column=0, sticky="nsw")
shiftLabel.grid(row=1, column=1, sticky="nsw")
shopLabel.grid(row=1, column=2, sticky="nsw")
#create dictionary of widgets
self.widgets = {
"Date":tk.Entry(AddShiftFrame),
"Shift":tk.Entry(AddShiftFrame),
"Shop":tk.Entry(AddShiftFrame)
}
#add widgets to frame
self.widgets["Date"].grid(row=2, column=0, sticky="nsw")
self.widgets["Shift"].grid(row=2, column=1, sticky="nsw")
self.widgets["Shop"].grid(row=2, column=2, sticky="nsw")
#this method will submit the data (callback function)
def submit_data(self):
shift_data = [(self.widgets["Date"].get()), (self.widgets["Shift"].get()), (self.widgets["Shop"].get())]
c.execute("INSERT INTO shifts (date, shift, shop) VALUES(?,?,?)", (shift_data[0], shift_data[1], shift_data[2]))
conn.commit()
print("done")
#submit button
submit = tk.Button(self, text="Submit",
command= lambda: submit_data(self))
submit.pack(padx=10, pady=10, side="left")
#home button
button1 = tk.Button(self, text="Home",
command=lambda: controller.showFrame(StartPage))
button1.pack(padx=10, pady=10, side='left')
app = MainApplication()
app.after(1000, getShifts)
app.mainloop()