1

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

4ntoine12
  • 11
  • 2
  • the if statement will be executed immediately, also you don't need to create a label each time a function is called and you are actually making it incorrectly (as with others, because layout manager methods return None), you simply need to `.config()` the widget instance, for example, just grid the button immediately but don't specify text (or use 0 as starting text), then simply do `label_instance.config(text=new_text)` – Matiiss Aug 23 '21 at 18:53
  • oh ok yeah that makes sense, I guess I was just thinking about it the wrong way, thanks for the awnser! – 4ntoine12 Aug 23 '21 at 18:56

4 Answers4

1

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

Block
  • 159
  • 1
  • 6
0

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

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...

Prakhar Parikh
  • 177
  • 2
  • 13
0

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:

enter image description here

Output after clicking:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19