Python 3.4 Tkinter - 对象没有该属性
我正在制作一个程序,想要模拟一个控制台。这是我到目前为止写的代码(我今天刚开始学习Tkinter,虽然我不确定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())
我不太确定如何使用command这个参数。
1 个回答
0
我不太确定在一个 Entry
组件上,command
参数应该做什么,但问题确实出在这里。command
需要一个可以调用的东西(也就是说,它需要一个函数的名字,而不是直接调用这个函数)。你现在程序的执行流程是:
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