2014-07-14 40 views
0

我一直在嘗試使用Tkinter和Python做一個簡單的程序。您所做的只是點擊按鈕,並根據您點擊的按鈕更新某些標籤。這是我的代碼:獲取一個簡單的Tkinter程序的錯誤

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 

我的理解,它可以不是一個整數轉換爲字符串,但我一直在嘗試一切,而且我似乎無法使它工作。是否有一種簡單的方法可以在窗口上顯示蘋果和黃金數字,並在每次點擊選擇或銷售按鈕時更新它們?謝謝。

回答

0

試圖將整數連接到字符串是什麼導致你的錯誤。您需要使用str function明確地將您的​​和gold整數變量轉換爲字符串。

替換:

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

有了:

label2 = Label(text = "Apples: " + str(apples)).pack() 
label3 = Label(text = "Gold: " + str(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() 
+0

謝謝大家對你的答案。這確實消除了錯誤。我以爲我曾經這樣做過。但是,仍然存在問題。每次點擊一個按鈕,它都不會更新新的蘋果或黃金標籤。任何方式來做到這一點?或者可以不工作? – Hero2016

+0

@ Hero2016我不確定如何在那裏幫助你,但是你可能需要添加一些代碼來改變你的'pick'和'sell'回調中的標籤。你可以發表一個關於這個新問題。 –

0

你的問題是,你試圖將字符串和整數:

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

這是您編輯的代碼:

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() 
相關問題