2015-11-02 77 views
0

所以我開始製作一個生存遊戲,但遇到了一個相當快的問題。我想製作一個按鈕,這個按鈕應該會去並且收集一定的時間(如在灌木叢中),並且當被擊中時,屏幕上顯示的灌木數量將增加15。但是每當我嘗試做時,數量的灌木從0到15,這是好的,但它不會更高。這裏是我目前的代碼:在Python中增加變量的麻煩3.4.3帶有標籤和按鈕的Tkinter

import tkinter as tk 

root = tk.Tk()     # have root be the main window 
root.geometry("550x300")  # size for window 
root.title("SURVIVE")   # window title 

shrubcount = 0 


def collectshrub(): 
    global shrubcount 
    shrubcount += 15 
    shrub.config(text=str(shrubcount)) 

def shrub(): 
    shrub = tk.Label(root, text='Shrub: {}'.format(shrubcount), 
      font=("Times New Roman", 16)).place(x=0, y=0) 


def shrubbutton(): 
    shrubbutton = tk.Button(root, command=collectshrub, text="Collect shrub", 
          font=('Times New Roman', 16)).place(x=0, y=200) 


shrub()    # call shrub to be shown 

root.mainloop()  # start the root window 

任何幫助都會很好!感謝噸 收到這些錯誤

shrub.config(text=str(shrub count)) 
AttributeError: 'NoneType' object has no attribute 'config' 
+0

你能否請刪除所有不必要的代碼,因爲你的問題實際上是在底部,這使得它很難找到。 – Inkblot

+0

@Inkblot沒關係 – SpencerG555

+0

如果你立刻打電話給'place()',則不需要調用'pack()'。挑一個或另一個。 –

回答

0

在您當前的代碼中,您在一個函數中定義了灌木,並且只在本地分配它時試圖在另一個函數中使用它。解決的辦法是徹底清除shrub()shrubbutton()功能,像這樣:

import tkinter as tk 

root = tk.Tk()    
root.geometry("550x300")   
root.title("SURVIVE")   

shrubcount = 0 

def collectshrub(): 
    global shrubcount 
    shrubcount += 15 
    shrub.config(text="Shrub: {" + str(shrubcount) + "}") 

    if shrubcount >= 30: 
    print(text="You collected 30 sticks") 
    craftbutton.pack() 

def craftbed(): 
#Do something 

shrub = tk.Label(root, text="Shrub: {" + str(shrubcount) + "}", font=("Times New Roman", 16)) 
shrub.place(x=0, y=0) 

shrubbutton = tk.Button(root, command=collectshrub, text="Collect shrub", font=('Times New Roman', 16)) 
shrubbutton.place(x=0, y=200) 

craftbutton = tk.Button(root, text="Craft bed", comma=craftbed) 

root.mainloop() 

ALSO 看到你在你的問題的底部,您的變量shrubcount被評爲功能shrub count得到了錯誤之後。這樣的小事可以完全改變你的代碼的工作方式。

+0

非常感謝你!!!!! – SpencerG555

+0

另一個快速的問題,對於'shrub.config'語句中的shrubcout,這是使它增加的那個,我如何回到那個說 '如果shrubcount> = 30:' '打印(「你收集了30根棍子」)' 我想指的是增加的shrubcount,而不是全球的增量0 – SpencerG555

+0

@ SpencerG555在我的文章的答案中添加。 – Inkblot

0

墨跡有正確的想法。

你灌木功能設定灌木的數量爲0。

點擊「收藏灌木」要求部隊灌木輸出shrubcount但在任何時間更新shrubcount變量。

一個清潔的版本的代碼,已經做了你想可能是這樣的:

import ttk 
from Tkinter import * 


class Gui(object): 
    root = Tk() 
    root.title("Survive") 
    shrubs = IntVar() 

    def __init__(self): 
     frame = ttk.Frame(self.root, padding="3 3 12 12") 
     frame.grid(column=0, row=0, sticky=(N, W, E, S)) 
     frame.columnconfigure(0, weight=1) 
     frame.rowconfigure(0, weight=1) 

     ttk.Label(frame, text="Shrubs:").grid(column=0, row=0, sticky=W) 
     ttk.Entry(frame, textvariable=self.shrubs, width=80).grid(column=0, row=2, columnspan=4, sticky=W) 
     ttk.Button(frame, command=self.add_shrubs, text="Get Shrubs").grid(column=6, row=3, sticky=W) 

    def add_shrubs(self): 
     your_shrubs = self.shrubs.get() 
     self.shrubs.set(your_shrubs + 15) 

go = Gui() 
go.root.mainloop() 

請注意,所有add_shrub確實是15增量shrubcount顯示灌木的數量由灌木標籤處理目的。

+0

將無法​​工作。只是給我0,因爲當它運行時它可能會改變add_shrub裏面的shrubcount,但它不會改變全局變量,所以shrubcount將在屏幕上保持爲0 – SpencerG555

+0

使用'shrub.config(text = str(shrubcount) )'每次在'add_shrub'下更新標籤 – Inkblot

+0

@MobiusCode我認爲它可能與使用全局變量有關,不知道但我嘗試過的所有東西都有效。只是不知道如何永久地改變一個像這樣的全局變量 – SpencerG555