我正在使用Tkinter Label小部件向我的UI框架显示一些文本,我希望Label每次单击按钮时都会更改文本。就我而言,我错了......它没有改变,是否可能?
这是我的代码..
currentCounterNumber = "0"
def counterPlus(teller_num):
#.... the data is working well ....
data = s.recv(1024)
if data:
currentCounterNumber = data
......
class Content(tk.Frame):
def __init__(self, master, teller_name,*args, **kwargs):
tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
self.L4 = tk.Label(self, text="Serving # " + currentCounterNumber +"!")
self.L4.pack( side = "top", fill="both", expand=False)
self.button1 = tk.Button(self, text="+", width=15, command=lambda: counterPlus(teller_no))
self.button1.pack(side = "top", fill="both", expand=True)
答案 0 :(得分:5)
假设content_obj = Content(....)
已定义。
您可以使用以下方式更改文字:
content_obj.L4['text'] = "Serving # {}!".format(currentCounterNumber)
或
content_obj.L4.configure(text="Serving # {}!".format(currentCounterNumber))
# OR config
from Tkinter import * # Python 3.x: from tkinter import *
def advance():
lb['text'] = str(int(lb['text']) + 1)
root.after(1000, advance)
root = Tk()
lb = Label(root, text='0')
lb.pack()
root.after(1000, advance)
root.mainloop()