python - "AttributeError: ' 描述 ' object has no attribute ' co_button '"与 tkinter

标签 python tkinter

我挣扎了大约 2 个小时知道..我看不出代码有什么问题。但这给了我这个错误。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\82104\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\82104\PycharmProjects\Macro\GUI.py", line 365, in on_entry_trace
    self.co_button['state']=new_state
AttributeError: 'Description' object has no attribute 'co_button'

我不知道第 1705 行的第一个错误是什么意思,但我可以从第二个错误中看到我的函数 on_entry_trace 找不到 self.co_button。但是我找不到为什么不能。我发现很多人都有这个问题,因为他们写的是这样的

button = ttk.Button(something).grid(something)

而不是

button = ttk.Button(something)
button.grid(something)

但这不是我的情况。

这是我遇到问题的类(class)。

class Description(tk.Toplevel):
    def __init__(self, master=None):
        super(Description, self).__init__(master=master)

        self.title('Description')
        self.grab_set()
        self.geometry('206x100')
        self.resizable(False, False)

        self.label_ds = ttk.Label(self, text='Enter Text').grid(column=0, row=0, pady=(12, 4))
        self.description = tk.StringVar()
        entry_ds = ttk.Entry(self, width=25, textvariable=self.description)
        entry_ds.grid(column=0, row=1, padx=13, pady=(0, 4))
        entry_ds.focus()
        self.description.trace('w', self.on_entry_trace)
        self.description.set("")

        self.co_button = ttk.Button(self, text='Confirm', command=self.on_press_ds)
        self.co_button.grid(column=0, row=2, pady=4)

        self.protocol("WM_DELETE_WINDOW", self.destroy_ds)

        self.wait_visibility()
        hide_minimize_maximize(self)

    def on_entry_trace(self, *args):
        new_state = "disabled" if self.description.get() == "" else "normal"
        self.co_button.configure(state=new_state)

    def on_press_ds(self):
        description = self.description.get()
        if description:
            self.master.listbox.insert('end', '-- ' + description + ' --')
        self.destroy_ds()
        self.destroy()

    def destroy_ds(self):
        self.master.ds_button['state'] = 'normal'
        self.destroy()

最佳答案

您收到此错误的原因是因为 Entry 小部件 self.description.trace('w', self.on_entry_trace) 的跟踪方法正在调用 self.on_entry_trace 声明 co_button 之前的方法。

I have no idea what the first error about line 1705 means

这是主 tkinter 库中的行,由于文件中的第 365 行,return self.func(*args) 无法执行。

<小时/>

这就是它的工作原理

Python 读起来就像......

    ....

    entry_ds.focus()
    # This will execute after above line 
    self.description.trace('w', self.on_entry_trace)
    # Then goes through self.on_entry_trace()
    ...
      # This will execute next
      new_state = "disabled" if self.description.get() == "" else "normal"
      # And here it'll throw an error as there is no attribute 
      self.co_button.configure(state=new_state)
    ...

    self.description.set("")
    ....

    # And you declared your button here
    self.co_button = ttk.Button(self, text='Confirm', command=self.on_press_ds)
    self.co_button.grid(column=0, row=2, pady=4)

因此,通过将 self.description.trace('w', self.on_entry_trace) 放在 __init__ 的末尾将修复错误并减少出现错误的可能性 future 会出现这样的错误。

<小时/>

提示:

bindstraceafter 的最佳方法放在代码末尾,以避免这些小错误。它还可能取决于您的策略。

关于python - "AttributeError: ' 描述 ' object has no attribute ' co_button '"与 tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56044290/

相关文章:

python - 如何用不同颜色的tkinter python制作不同的单词?

Python 3.4 在 Apache2 服务器 (Mac) 上导致 UnicodeEncodeError 但在命令行中工作正常

python - FastAPI 删除调用的 API 响应

python - Python 和 Tomcat 之间缺少 PUT 变量

python - 在 Django 中使用 API 调用创建新模型实例

python - AttributeError: 图片没有属性 'open'

python - `tkinter. Spinbox()` 的行为如何取决于传递给其 `textvariable` 的值的类型

python - ubuntu9.10 : how to use python's lib-dynload and site-packages directories?

python - 当加载图标并且 tk.mainloop 在线程中时,Tkinter 锁定 Python

python - Windows 中没有名为 'tkinter' (Python3.8) 的模块