Reputation: 11
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
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
Upvotes: 1
Views: 1968
Reputation: 4595
Just added rankDisplay.configure(...)
in line 16.
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 after clicking:
Upvotes: 0
Reputation: 179
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
Upvotes: 1
Reputation: 197
See here you should use the update
method for the label named rankDisplay
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.
and in the rankDisplay
label you have to use textvariable
attribute instead of text
only these are the changes...
Upvotes: 0
Reputation: 57
I already knew this problem. To solve this, you have to create a global variable to your label :
global l1
l1 = Label(...)
Then, to modify the text, you have to do in your fonction :
l1.config(text=str(count))
Upvotes: 1