STRhythm
STRhythm

Reputation: 113

Incrementing variable by 1 when button is pressed

I've been trying to figure out tkinter as I need it for my computer science project so I'm still very new to it but I'm trying to make a button that when pressed, a variable is increased and outputted with the changed amount.

This was the original code I tried:

from tkinter import *

root = Tk()

item_num = 0


def AddButton():
    global item_num
    item_num =+ 1
    item_text = "Item "+ item_num
    item1 = Label(receiptFrame, text=item_text, padx=25, pady=10)
    item1.pack()

addButton = Button(root, text="Add Button", padx=25, pady=25, command=AddButton)
addButton.pack()

receiptFrame = Frame(root, width=70, height=30)
receiptFrame.pack()

root.mainloop

When I press the button this comes up:

item_text = "Item "+ item_num

TypeError: can only concatenate str (not "int") to str

I tried using another solution I found on here using IntVar(), set & get but when run it I get:

TypeError: can only concatenate str (not "IntVar") to str

from tkinter import *

root = Tk()

item_num = 0
item_num = IntVar()

def AddButton():
    global item_num
    item_num.set(item_num.get()+1)
    item_num = int(item_num)
    item_text = "Item "+ item_num
    item1 = Label(receiptFrame, text=item_text, padx=25, pady=10)
    item1.pack()

addButton = Button(root, text="Add Button", padx=25, pady=25, command=AddButton)
addButton.pack()

receiptFrame = Frame(root, width=70, height=30)
receiptFrame.pack()

root.mainloop

I also did try putting item_num into the function as a parameter using lambda but when I did that the button couldn't be pressed at all so again, I'm not sure what went wrong there

Again, I'm not entirely sure what I'm doing so I'd appreciate any semi-beginner solutions aha :)

Upvotes: 0

Views: 2483

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

You are using + for concatenation, which is the problem because both side of the + requires to be a string. Also you had other problems, like:

  • You used =+ 1, which means the value is +1(positive 1) always, which should be += 1 so it gets increased by 1 each time.
  • Also you forgot to put () at the end of mainloop().
  • You are creating a new label each time you click on the button, instead make a label and change its text.

So the final formatted code is:

from tkinter import *

root = Tk()

item_num = 0
def AddButton():
    global item_num

    item_num += 1 # Add current value plus 1
    item_text = "Item"+str(item_num) 
    item1.config(text=item_text) # Change the text of the label
    print(item_text)

item1 = Label(root, padx=25, pady=10) # Make the label for the first time
item1.pack()
# Same code...

root.mainloop() # Important ()

There are other ways you can use write item_text, like:

item_text = "Item"+str(item_num) # This returns a string
item_text = "Item",item_num # This returns a tuple
item_text = f'Item{item_num}' # This returns a string
item_text = "Item{}".format(item_num) # This returns a string as well

Upvotes: 3

Related Questions