'How to get my button to call a function and count from tkinter?
I'm trying to call a function at every button click, the function is calling a text from a entrybox and transferring it to new label and every time I click a button the number must increase\increment by 1' the code runs until I press the button the gives me this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\NdohAdmin\PycharmProjects\gui\main.py", line 24, in button_clicked
new += 1
TypeError: can only concatenate str (not "int") to str
This is the code:
from tkinter import *
window = Tk()
window.geometry("500x200")
window.title("My first GUI program")
# label head
Head = Label(text="WELCOME TO THE SKIPPING CHALLENGE", font=("Arial", 30, "bold"))
Head.Grid(row=0, column=1)
counter = Label(text="Enter Your target.", font=("Arial", 20, "bold",))
counter.grid()
# Entry box
msay = Entry(width=10)
msay.grid()
msay.get()
# Function to increase
def button_clicked():
global new
new = msay.get()
new += 1
# Button calls a function
button = Button(text="Skip to count", command=button_clicked)
button.grid()
skipping_count = Label(text="Your Counter", font=("Arial", 20, "bold"))
skipping_count.grid()
new = Label(text="0", font=("Arial", 20, "bold"))
new.grid()
window.mainloop()
Solution 1:[1]
msay.get() gives you a string type number, You need to convert it to an integer using int()
from tkinter import *
window = Tk()
window.geometry("500x200")
window.title("My first GUI program")
# label head
Head = Label(text="WELCOME TO THE SKIPPING CHALLENGE", font=("Arial", 30, "bold"))
Head.grid(row=0, column=1)
counter = Label(text="Enter Your target.", font=("Arial", 20, "bold",))
counter.grid()
# Entry box
msay = Entry(width=10)
msay.grid()
msay.get()
# Function to increase
def button_clicked():
global new
new = int(msay.get())
new += 1
# Button calls a function
button = Button(text="Skip to count", command=button_clicked)
button.grid()
skipping_count = Label(text="Your Counter", font=("Arial", 20, "bold"))
skipping_count.grid()
new = Label(text="0", font=("Arial", 20, "bold"))
new.grid()
window.mainloop()
Your question is not clear to me, But I write what I understand.
I think this could help you.
from tkinter import *
window = Tk()
window.geometry("500x200")
window.title("My first GUI program")
# label head
Head = Label(text="WELCOME TO THE SKIPPING CHALLENGE", font=("Arial", 30, "bold"))
Head.grid(row=0, column=1)
counter = Label(text="Enter Your target.", font=("Arial", 20, "bold",))
counter.grid()
# Entry box
msay = Entry(width=10)
msay.grid()
msay.get()
start = 0
v = 0
# Function to increase
def button_clicked():
global start,v # Excess to global variable.
if msay.get() == '': # Change the value of start to zero and insert 0 inside entry if there is no value inside the entry.
start=0
msay.insert(0,'0')
elif v != int(msay.get()): # Check if value inside entry is change, Then change the value of v and start.
v = int(msay.get())
start = int(msay.get())
new['text'] = start # Change the text of the label
start+=1 # add 1 to start.
# Button calls a function
button = Button(text="Skip to count", command=button_clicked)
button.grid()
skipping_count = Label(text="Your Counter", font=("Arial", 20, "bold"))
skipping_count.grid()
new = Label(text="0", font=("Arial", 20, "bold"))
new.grid()
window.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
