【问题标题】:Python 3.4 Tkinter - Object has no attributePython 3.4 Tkinter - 对象没有属性
【发布时间】:2014-09-10 15:13:53
【问题描述】:

我正在制作一个程序来模拟控制台。这是我目前的代码(我今天开始使用它,学习 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 参数。

【问题讨论】:

  • 从你的 command 参数中删除括号,它将解决这两个错误
  • 入口小部件没有命令参数?你确定你说的不是 validatecommand?

标签: python tkinter attributeerror


【解决方案1】:

我不确定command 参数应该Entry 小部件上做什么,但这就是问题所在。 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多