2014-09-10 93 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 
相关问题