How to increment number from a button click

0

I want a button that adds '' +1 '' to the label that is displayed in the tkinter Window, but I do not know very well how to do it and I did not find anything on Google, what I understood did not return what I 1x it changes the value to 1, press again it changes the value to 2, again to 3 (adding +1 every time it is clicked), and instead with " - "decreasing this same number 3,2,1 if necessary

from tkinter import*
janela = Tk()
text = 0


def bt7_click(): #botao +
    soma =(text)
    lb4["text"] = str(text + 1)

def bt8_click():
    soma =(text)
    lb4["text"] = str(text - 1)



lb4 = Label(janela,text="0",font="Arial 50", fg= "red", bg="white")
lb4.place(x=50, y=50)


bt7 = Button(janela, width=1, text="+", command=bt7_click)
bt7.place(x=130, y=90)
bt8 = Button(janela, width=1, text="-", command=bt8_click)
bt8.place(x=150, y=90)


janela.geometry("200x200")
janela.mainloop()
    
asked by anonymous 28.04.2017 / 08:05

1 answer

1

Good morning,

For you to be able to do this increment and decrement control you will need an accumulator

You can use the field itself as an accumulator, for example

With some minor modifications to your code, you can do it as follows:

from tkinter import*
janela = Tk()

limite = 0

def bt7_click(): #botao +
    val = int(lb4["text"])
    lb4["text"] = str(val + 1)

def bt8_click():
    val = int(lb4["text"])
    if val <= limite:
        val = limite + 1
    lb4["text"] = str(val - 1)


lb4 = Label(janela,text="0",font="Arial 50", fg= "red", bg="white")
lb4.place(x=50, y=50)
bt7 = Button(janela, width=1, text="+", command=bt7_click)
bt7.place(x=130, y=90)
bt8 = Button(janela, width=1, text="-", command=bt8_click)
bt8.place(x=150, y=90)

janela.geometry("200x200")
janela.mainloop()

lb4["text"] = '0'
    
28.04.2017 / 14:18