2014-09-10 105 views
0

我在製作一個程序來模擬一個控制檯。這是到目前爲止我的代碼(我現在就開始吧,學習Tkinter的,雖然我不知道,如果Tkinter的最佳選擇):Python 3.4 Tkinter - 對象沒有屬性

from tkinter import * 

class App: 
    def __init__(self, master): 
     self.frame = Frame(master, bg = 'black') 
     self.bottomframe = Frame(master, bg = 'black') 
     self.elabel = Label(master, text = '>', bg = 'black', font = 'system', fg = 'white') 
     self.einput = Entry(master, bd =0, bg = 'black', font = 'system', fg = 'white', command = self.update_text()) 

     # Packing 
     self.frame.pack() 
     self.bottomframe.pack(side = BOTTOM) 
     self.elabel.pack(side = LEFT) 
     self.einput.pack(side = LEFT) 
    def update_text(self): 
     self.einput.insert(0, '>') 


root = Tk() 
app = App(root) 
root.mainloop() 

的錯誤是

Traceback (most recent call last): 
    File "C:/Users/*/PycharmProjects/ConsoleDungeon/game.py", line 21, in <module> 
    app = Game(root) 
    File "C:/Users/*/PycharmProjects/ConsoleDungeon/game.py", line 9, in __init__ 
    self.einput = Entry(master, bd =0, bg = 'black', font = 'system', fg = 'white', command = self.update_text()) 
    File "C:/Users/*/PycharmProjects/ConsoleDungeon/game.py", line 17, in update_text 
    self.einput.insert(0, '>') 
AttributeError: 'Game' object has no attribute 'einput' 

還有一件事:在線路

self.einput = Entry(master, bd =0, bg = 'black', font = 'system', fg = 'white', command = self.update_text()) 

我不知道如何使用命令說法。

+0

從你的'command'參數去掉括號,這將解決這兩個錯誤 – 2014-09-10 15:17:10

+0

輸入構件沒有一個命令參數?你確定你不是指validatecommand? – 2014-09-10 15:20:10

回答

0

我不知道一個command的說法是什麼應該到上Entry部件做,但是這也正是問題的生活好起來。 command需要一個可調用的(即一個函數NAME,而不是該函數的調用)。你的程序,現在流的方法是:

create App 
make App.frame 
make App.bottomframe 
make App.elabel 
make App.einput 
--> Oh, there's a function call in that declaration, gotta follow it before I 
    finish making this object 
------> The function call refers to an `App.einput`, but I don't have one of 
     those (yet!) so we better throw an AttributeError 
相關問題