[英]Tkinter label overwrite: update or refresh?
I am building a schedulemaker in Python with a GUI using Tkinter. 我正在使用Tkinter用GUI在Python中构建一个调度程序。
I basically have the same problem as in this question : the labels on the schedule table don't get replaced; 我基本上有与这个问题相同的问题:时间表表上的标签不会被替换; another one comes on top.
另一个在上面。 But since I dynamically create the table I don't have variable names for the labels.
但是由于我是动态创建表的,所以没有标签的变量名。
So can I still update the text value of the label widgets without a variable name? 因此,我仍然可以在没有变量名的情况下更新标签小部件的文本值吗? Is there a way with StringVar to do this?
StringVar有办法做到这一点吗? Or how do I properly refresh the table?
或者如何正确刷新表?
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()
The solution is to create the label widgets once, save a reference to each widget, and then change the widget rather than create new widgets. 解决方案是一次创建标签窗口小部件,保存对每个窗口小部件的引用,然后更改窗口小部件,而不是创建新窗口小部件。
Since you seem to be building a table-like structure, use a (row,column) tuple to store the widgets in a dictionary. 由于您似乎正在构建类似表格的结构,因此请使用(行,列)元组将小部件存储在字典中。 For example:
例如:
#(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
Later, you can change the label using configure
. 以后,您可以使用
configure
更改标签。 For example, to change the label for row 1, column 10 you would do: 例如,要更改第1行第10列的标签,您可以执行以下操作:
widgets[(1,10)].configure(text="the new text")
You should always assign a label with a name and if you want to change or update the value of a label use config()
您应该始终为标签分配一个名称,如果要更改或更新标签的值,请使用
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)
Then Use config()
or set()
if you wish to update the label Minor changes, just naming each variable, you can change it if you want. 然后,如果要更新标签“ Minor changes”,只需使用每个变量命名,然后使用
config()
或set()
即可进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.