2015-11-12 314 views
0

我在使用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() 

回答

0

解決方案是創建標籤小部件一次,保存對每個小部件的引用,然後更改小部件而不是創建新的小部件。

由於您似乎在構建一個類似於表的結構,因此請使用(行,列)元組將元素存儲在字典中。例如:

#(Re)Build schedule on screen 
def BuildSchedule(): 
    global widgets 
    widgets = {} 

    for r in range(1,4): 
     label = Label(ScheduleFrame, text=stafflist[r-1].name) 
     label.grid(row=r, column=0) 
     widgets[(r,0)] = label 

    for c in range(1,15): 
     label = Label(ScheduleFrame, text=weekdays[(c-1)%7]) 
     label.grid(row=0, column=c) 
     widgets[(0,c)] = label 

    for r in range(1,4): 
     for c in range(1,15): 
      label = Label(ScheduleFrame, text=scheduleDictionary[r-1][c-1]) 
      label.grid(row=r, column=c) 
      widgets[(r,c)] = label 

之後,您可以使用configure更改標籤。例如,要更改第1行第10列的標籤,您應該這樣做:

widgets[(1,10)].configure(text="the new text") 
+0

謝謝,這工作!我在想的關鍵是使用元組來獲取變量中的行和列信息。 – Bob

-1

你應該始終分配一個標籤與一個名字,如果你想更改或更新標籤的值使用config()

import tkinter 
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): 
     abcd = tkinter.Label(ScheduleFrame, text=stafflist[r-1].name).grid(row=r, column=0) 

    for c in range(1,15): 
     efgh = tkinter.Label(ScheduleFrame, text=weekdays[(c-1)%7]).grid(row=0, column=c) 

    for r in range(1,4): 
     for c in range(1,15): 
      ijkl = tkinter.Label(ScheduleFrame, text=scheduleDictionary[r-1][c-1]).grid(row=r, column=c) 

然後,如果你要更新的標籤使用config()set() 微小的變化,只是命名每個變量,你可以改變它,如果你想。

+0

您的更改不是很有用。你只需要覆蓋變量'abcd' 4次,'efgh' 15次,'ijkl' 60次。最後,你只有三個變量來表示所有的小部件。 –