Python 获取简单Tkinter程序的错误

Python 获取简单Tkinter程序的错误,python,tkinter,Python,Tkinter,我一直在尝试使用Tkinter和Python编写一个简单的程序。您只需单击按钮,它会根据您单击的按钮更新某些标签。这是我的代码: from tkinter import * apples = 0 gold = 0 def pick(): global apples apples = apples + 1 def sell(): global apples global gold gold = gold + (apples * 10) app

我一直在尝试使用Tkinter和Python编写一个简单的程序。您只需单击按钮,它会根据您单击的按钮更新某些标签。这是我的代码:

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()
现在,每当我尝试运行程序时,我都会得到错误:

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

我知道它不能将整数转换成字符串,但我已经尝试了所有的方法,但我似乎无法让它工作。有没有一种简单的方法可以在窗口上显示苹果和黄金的数字,并让它们在我每次单击“选择或出售”按钮时更新?谢谢。

尝试将整数连接到字符串是导致错误的原因。您需要使用

替换:

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

固定源代码:

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()

您的问题是,您正在尝试连接字符串和整数:

label2 = Label(text = "Apples: " + apples).pack()
label3 = Label(text = "Gold: " + gold).pack()
这会产生一个错误:

>>> apples = 0
>>> "Apples: ", apples
('Apples: ', 0)
>>> "Apples: " + apples
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> 

谢谢大家的回答。这确实消除了错误。我以为我以前做过。但是,仍然存在一个问题。每次我点击一个按钮,它不会用新数量的苹果或黄金更新标签。有什么办法可以做到这一点吗?或者这不起作用吗?@Hero2016我不知道如何帮助您,但您可能需要添加一些代码来更改“选择和销售”回调中的标签。你可以发布一个关于这个的新问题。
>>> apples = 0
>>> "Apples: ", apples
('Apples: ', 0)
>>> "Apples: " + apples
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> 
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()