Getting an error for a simple Tkinter program
Question: I have been trying to make a simple program using Tkinter and Python. All you
do is click buttons and depending on what button you click, it updates certain
labels. This is my code:

    
    
    from tkinter import *
    
    apples = 0
    gold = 0
    
    def pick():
        global apples
        apples = apples + 1
    
    
    def sell():
        global apples
        global gold
        gold = gold + (apples * 10)
        apples = 0
    
    app = Tk()
    
    app.title("Apple Picking Simulator 2014")
    app.geometry("400x300+100+60")
    
    label1 = Label(text = "Welcome to Apple Picking Simulator 2014!").pack()
    Label().pack()
    label2 = Label(text = "Apples: " + apples).pack()
    label3 = Label(text = "Gold: " + gold).pack()
    button1 = Button(text = "Pick Apple", command = pick).pack()
    button2 = Button(text = "Sell Apples", command = sell).pack()
    
    app.mainloop()
    

Now, whenever I try to run the program, I just get the error:

    
    
    TypeError: Can't convert 'int' object to str implicitly
    

I understand that it can't convert an integer to a string, but I've been
trying everything, and I can't seem to make it work. Is there an easy way to
display the apple and gold numbers on the window and have them update every
time I click the pick or sell button? Thanks.


Answer: Trying to concatenate an integer to a string is what is causing your error.
You need to explicitly cast your `apples` and `gold` integer variables to a
string using the [`str`
function](https://docs.python.org/2/library/functions.html#str).

Replace:

    
    
    label2 = Label(text = "Apples: " + apples).pack()
    label3 = Label(text = "Gold: " + gold).pack()
    

With:

    
    
    label2 = Label(text = "Apples: " + str(apples)).pack()
    label3 = Label(text = "Gold: " + str(gold)).pack()
    

Fixed source code:

    
    
    from tkinter import *
    
    apples = 0
    gold = 0
    
    def pick():
        global apples
        apples = apples + 1
    
    
    def sell():
        global apples
        global gold
        gold = gold + (apples * 10)
        apples = 0
    
    app = Tk()
    
    app.title("Apple Picking Simulator 2014")
    app.geometry("400x300+100+60")
    
    label1 = Label(text = "Welcome to Apple Picking Simulator 2014!").pack()
    Label().pack()
    label2 = Label(text = "Apples: " + str(apples)).pack()
    label3 = Label(text = "Gold: " + str(gold)).pack()
    button1 = Button(text = "Pick Apple", command = pick).pack()
    button2 = Button(text = "Sell Apples", command = sell).pack()
    
    app.mainloop()
    

