Dan
Dan

Reputation: 159

How to use GUI Tkinter button to increment a number and display it

I'm new to trying out python GUI's and tried tkinter and pyglet, but only through tutorials, in-order-to understand the basic classes and functions. But what I'm currently trying to do is to get a button to increase a number whilst displaying that number at the same time. Somehow, even though the variable number was stated globally as 0, the function to increase it doesn't do anything, it actually produces an error: 'UnboundLocalError: local variable 'number' referenced before assignment'. I have no idea how to correct this.

The tutorials I've seen on both YouTube and as an article, don't talk about how to do this exactly. The article does mention how to change a certain text though, but not a previously created variable (which in my case would be 'number').

from tkinter import *
number = 0

window = Tk()
window.title("Programme")
window.geometry('350x250')

label = Label(window, text=number)
label.grid(column=0,row=0)

def clicked():
number += 1

button = Button(window, text="Push Me", command=clicked)
button.grid(column=1, row=2)

window.mainloop()

Is there any way to do this? Also I've been looking for how to add time, to handle events and such, through ticks. But everything I find on the internet is about literally displaying a clock on the GUI, which is useless, or at least I don't know how to use it to have a ticking function.

Upvotes: 1

Views: 7308

Answers (2)

Anılcan
Anılcan

Reputation: 1

Here is my entire code:

from tkinter import *

def up():
    number.set(number.get()+1)

def down():
    number.set(number.get()-1)

window = Tk()
window.title("Programme")
window.geometry('350x250')
number = IntVar()

frame = Frame(window)
frame.pack()

entry = Entry(frame, textvariable=number, justify='center')
entry.pack(side=LEFT, ipadx=15)

buttonframe = Frame(entry)
buttonframe.pack(side=RIGHT)

buttonup = Button(buttonframe, text="▲", font="none 5", command=up)
buttonup.pack(side=TOP)

buttondown = Button(buttonframe, text="▼", font="none 5", command=down)
buttondown.pack(side=BOTTOM)

window.mainloop()

It looks better for me when the buttons are inside of the entry widget directly.

Upvotes: 0

Novel
Novel

Reputation: 13729

You need to increment the number, like you do, but also update the Label to display the new number:

from tkinter import *
number = 0

window = Tk()
window.title("Programme")
window.geometry('350x250')

label = Label(window, text=number)
label.grid(column=0,row=0)

def clicked():
    global number
    number += 1
    label.config(text=number)

button = Button(window, text="Push Me", command=clicked)
button.grid(column=1, row=2)

window.mainloop()

An easier way to do this is to use tkinter's version of an integer: IntVar. It takes care of the Label updates automatically, but it requires you use get() and set() to work with it.

from tkinter import *

def clicked():
    number.set(number.get()+1)

window = Tk()
window.title("Programme")
window.geometry('350x250')
number = IntVar()

label = Label(window, textvariable=number)
label.grid(column=0,row=0)

button = Button(window, text="Push Me", command=clicked)
button.grid(column=1, row=2)

window.mainloop()

Upvotes: 4

Related Questions