我在使用Tkinter的GUI中構建一個Python的schedulemaker。Tkinter標籤覆蓋:更新或刷新?
我基本上有和this question一樣的問題:日程表上的標籤不被替換;另一個是最重要的。但是由於我動態地創建了表格,我沒有標籤的變量名稱。
那麼我還可以更新沒有變量名稱的標籤小部件的文本值嗎?有沒有與StringVar做到這一點?或者我如何正確刷新表格?
from tkinter import *
#DATA
class Staff(object):
def __init__(self, name, ID):
self.name = name #this data comes from storage
self.ID = ID #this is for this instance, starting from 0 (for use with grid)
ID42 = Staff("Joe", 0)
ID25 = Staff("George", 1)
ID84 = Staff("Eva", 2)
stafflist = [ID42, ID25, ID84]
weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
scheduleDictionary = {}
for r in range(0,3):
scheduleDictionary[r] = ['shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift','shift']
#Build window
root = Tk()
ScheduleFrame = Frame(root)
ScheduleFrame.pack()
#(Re)Build schedule on screen
def BuildSchedule():
for r in range(1,4):
Label(ScheduleFrame, text=stafflist[r-1].name).grid(row=r, column=0)
for c in range(1,15):
Label(ScheduleFrame, text=weekdays[(c-1)%7]).grid(row=0, column=c)
for r in range(1,4):
for c in range(1,15):
Label(ScheduleFrame, text=scheduleDictionary[r-1][c-1]).grid(row=r, column=c)
#Mouse events
def mouse(event):
y = event.widget.grid_info()['row'] - 1
x = event.widget.grid_info()['column'] - 1
print(x,y)
shiftSelection(y,x)
#shiftSelection
def shiftSelection(row, column):
shifts_window = Tk()
box = Listbox(shifts_window)
box.insert(1, "MR")
box.insert(2, "AR")
box.insert(3, "ER")
box.pack()
button = Button(shifts_window, text="Okay", command = lambda: selectShift(shifts_window, box.get(ACTIVE),row, column))
button.pack()
def selectShift(shifts_window, shift,row, column):
scheduleDictionary[row][column] = shift
BuildSchedule()
shifts_window.destroy()
root.bind("<Button-1>", mouse)
BuildSchedule()
root.mainloop()
謝謝,這工作!我在想的關鍵是使用元組來獲取變量中的行和列信息。 – Bob