-1

I have the code off a clicker below and I'm trying to solve how to show to the number of clicks on the window in tkinter. I'm new to python. If I click on the button the number of clicks remains the same. I don't know if the text doesn't updates or if the increase() function doesn't work. Please help me solve this.

from tkinter import *

clicks = 0
def increase(clicks):
    clicks += 1

root = Tk()
root.geometry('500x300')
label=Label(text="Clicks:")
show=Label(text=clicks)
btc = Button(text="Click me", command=increase(clicks))

label.pack()
show.pack()
btc.pack()

root.mainloop()
pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
Michael
  • 27
  • 2
  • 6
  • 1
    Along with [Using global variables in a function](//stackoverflow.com/q/423379) – Aran-Fey May 26 '18 at 18:40
  • 1
    And the next thing you'll be interested in will be [How to make a Tkinter label update?](//stackoverflow.com/q/28303346) – Aran-Fey May 26 '18 at 18:41

1 Answers1

0

You need to set the label to have a variable of a particular kind called an IntVar()

Then use .set() and .get() to change the variable value and get its value. When it is changed then the label is automatically.

I suggest you have a look at this link.

Working Code:

from tkinter import *

def increase():
    clicks.set(clicks.get() + 1)

root = Tk()
root.geometry('500x300')
label=Label(root, text="Clicks:")
clicks = IntVar()
show=Label(root, textvariable=clicks)
btc = Button(root, text="Click me", command=increase)

label.pack()
show.pack()
btc.pack()

root.mainloop()
Flaming_Dorito
  • 486
  • 3
  • 11