python Tkinter标签通过按钮命令更改文本值 [英] python Tkinter label changing text value by Button Command

查看:513
本文介绍了python Tkinter标签通过按钮命令更改文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tkinter标签窗口小部件在UI框架中显示一些文本,并且我希望标签每次单击按钮时都可以更改文本.就我而言,我错了...它没有改变,可能吗?

I am using Tkinter Label widget to display some text to my UI Frame and I want the Label to change the text every-time I click the button. In my case I got wrong... it didn't change, is it possible?

这是我的代码.

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_obj = Content(....).

您可以使用来更改文本:

You can change the text using :

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()

这篇关于python Tkinter标签通过按钮命令更改文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆