[英]How to update tkinter label
So I'm trying to make some sort of basic auto clicker with a rank that updates after a certain amount of clicks, but whenever I update the rank the label that is supposed to display it doesn't change and I don't know how to make the label update所以我正在尝试制作某种基本的自动答题器,其排名会在一定数量的点击后更新,但每当我更新排名时,应该显示的 label 不会改变,我不知道如何进行 label 更新
from tkinter import *
count = 0
rank = "click the button to rank up!"
window = Tk()
if count == 1:
rank = "wow first click!"
def click():
global count
count += 1
counter = Label(window, text=count).grid(row = 0, column = 1)
clicker = Button(window, text="The Button", padx = 50, pady = 50, command = click).grid(row = 0, column = 0)
rankDisplay = Label(window, text = rank, padx = 100, pady = 25).grid(row = 1, column = 0)
window.mainloop()
after clicking for the first time, the rank is still displayed as "click the button to rank up" instead of "wow first click", and that's pretty much the issue第一次点击后,排名仍然显示为“点击按钮排名”而不是“哇第一次点击”,这就是问题所在
This is code:这是代码:
from tkinter import *
count = 0
window = Tk()
def changed(text):
rankDisplay.config(text=text)
rankDisplay.grid(row = 1, column = 0)
def click():
global count
count += 1
counter = Label(window, text=count).grid(row = 0, column = 1)
if count == 1:
changed("wow first click!")
return count
clicker = Button(window, text="The Button", padx = 50, pady = 50, command = click).grid(row = 0, column = 0)
rankDisplay = Label(window, text = "", padx = 100, pady = 25)
changed("click the button to rank up!")
window.mainloop()
When you click the button then label text is update "click buttton to rank up" to "wow first click".Because The text of label is a StringVar() and if I set the stringvar then the text of label is update to stringvar当您单击按钮时,label 文本将“单击按钮排名”更新为“哇第一次点击”。因为 label 的文本是一个 StringVar(),如果我设置 stringvar,则 label 的文本将更新为 stringvar
I already knew this problem.我已经知道这个问题了。 To solve this, you have to create a global variable to your label:要解决这个问题,您必须为您的 label 创建一个全局变量:
global l1
l1 = Label(...)
Then, to modify the text, you have to do in your fonction:然后,要修改文本,您必须在函数中执行以下操作:
l1.config(text=str(count))
See here you should use the update
method for the label named rankDisplay
看到这里你应该使用名为rankDisplay
的 label 的update
方法
here is the code:这是代码:
from tkinter import *
count = 0
window = Tk()
rank = StringVar()
rank.set("Click the button to rank up")
def click():
global count
count += 1
counter = Label(window, text=count).grid(row=0, column=1)
if count == 1:
rank.set("wow first click!")
rankDisplay.update()
clicker = Button(window, text="The Button", padx=50, pady=50, command=click).grid(row=0, column=0)
rankDisplay = Label(window, textvariable=rank, padx=100, pady=25)
rankDisplay.grid(row=1, column=0)
window.mainloop()
Modifications in your code: you should know the concept of StringVar
in tkinter to use the update
method.. link to know it https://www.pythontutorial.net/tkinter/tkinter-stringvar/#:~:text=The%20Tkinter%20StringVar%20helps%20you,Label%20or%20Entry%20more%20effectively.&text=The%20StringVar%20constructor%20accepts%20three,defaults%20to%20the%20root%20window .代码中的修改:您应该知道tkinter中StringVar
的概念才能使用update
方法..链接了解它https://www.pythontutorial.net/tkinter/tkinter-stringvar/#:~:text=The%20Tkinter %20StringVar%20helps%20you,Label%20or%20Entry%20more%20effectively.&text=The%20StringVar%20constructor%20accepts%20three,defaults%20to%20the%20root%20window 。
and in the rankDisplay
label you have to use textvariable
attribute instead of text
在rankDisplay
label 你必须使用textvariable
属性而不是text
only these are the changes...只有这些是变化......
Just added rankDisplay.configure(...)
in line 16.刚刚在第 16 行添加了rankDisplay.configure(...)
。
Code:代码:
from tkinter import *
count = 0
rank = "click the button to rank up!"
window = Tk()
#if count == 1:
#rank = "wow first click!"
def click():
global count
count += 1
counter = Label(window, text=count)
counter.grid(row = 0, column = 1)
rankDisplay.configure(text="wow first click!")
clicker = Button(window, text="The Button", padx = 50, pady = 50, command = click).grid(row = 0, column = 0)
rankDisplay = Label(window, text = rank, padx = 100, pady = 25)
rankDisplay.grid(row = 1, column = 0)
window.mainloop()
Output: Output:
Output after clicking:点击后Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.