Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python Tkinter标签通过按钮命令更改文本值_Python_Tkinter_Label - Fatal编程技术网

python Tkinter标签通过按钮命令更改文本值

python Tkinter标签通过按钮命令更改文本值,python,tkinter,label,Python,Tkinter,Label,我正在使用Tkinter标签小部件向我的UI框架显示一些文本,我希望每次单击按钮时标签都会更改文本。就我而言,我错了。。。它没有改变,可能吗 这是我的密码 currentCounterNumber = "0" def counterPlus(teller_num): #.... the data is working well .... data = s.recv(1024) if data: currentCounterNumber = data .

我正在使用Tkinter标签小部件向我的UI框架显示一些文本,我希望每次单击按钮时标签都会更改文本。就我而言,我错了。。。它没有改变,可能吗

这是我的密码

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)

假设定义了
content\u obj=content(..)

您可以使用以下方法更改文本:

content_obj.L4['text'] = "Serving # {}!".format(currentCounterNumber)

例子:
假设定义了
content\u obj=content(..)

您可以使用以下方法更改文本:

content_obj.L4['text'] = "Serving # {}!".format(currentCounterNumber)

例子:
感谢您的回复falsetru,您有更改标签文本的按钮命令示例吗?@gadss,抱歉,但我不明白您的要求。@gadss,示例代码:如果您单击按钮,标签文本将更改。我尝试添加
content_obj.L4['text']=“service#{}!”格式(currentCounterNumber)
在我的
def counterPlus(teller_num)
上,但似乎
self.L4
标签没有更改文本值感谢replu falsetru,是否有更改标签文本的按钮命令示例?@gadss,对不起,我不明白您的要求。@gadss,示例代码:如果您单击按钮,标签的文本已更改。我尝试在我的
def counterPlus(teller_num)
上添加
content_obj.L4['text']=“service{}format(currentCounterNumber)
,但似乎
self.L4
标签没有更改文本值将
counterPlus
方法移动到
content
类,添加
Content.L4
作为
counterPlus
的参数,或在
Content
中创建
changeL4Text(newText)
方法,因为您需要访问它来更改其文本。将
counterPlus
方法移动到
Content
类,添加
Content.L4
作为
counterPlus
的参数,或在
Content
中创建
changeL4Text(newText)
方法,因为您需要访问它来更改其文本。
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()