ttk.Button 返回 None [英] ttk.Button returns None
问题描述
我正在尝试使用 ttk.Button 的 invoke 方法,如 所示TkDocs(查看命令回调"),但我不断收到此错误:
I am trying to use the invoke method of a ttk.Button, as shown at TkDocs (look at "The Command Callback"), but I keep getting this error:
AttributeError: 'NoneType' 对象没有属性 'invoke'
AttributeError: 'NoneType' object has no attribute 'invoke'
所以,我在 Interactive Shell 中尝试了这个:
So, I tried this in the Interactive Shell:
ActivePython 3.1.1.2 (ActiveState Software Inc.) based on
Python 3.1.1 (r311:74480, Aug 17 2009, 12:30:13) [MSC v.1500 32 bit (Intel)] on
win32
>>> from tkinter import *
>>> import tkinter.ttk as ttk
>>> root = Tk()
>>> button = ttk.Button(root, text="Test").grid(row=0, column=0)
>>> print(button)
None
这表明 ttk.Button 返回 None.
Which shows that ttk.Button returns None.
ttk.Button 是否意味着返回 None.而且,如果是这样,为什么 TkDocs 说有一个调用方法?
Is ttk.Button meant to return None. And, if so, why does TkDocs say that there is an invoke method?
推荐答案
不,你完全错了:你的代码没有不显示 ttk.Button
返回 None
-- 显示按钮对象上的grid
方法返回None
!难道你没有看到你正在对 ttk.Button
返回的任何内容(按钮对象)调用 .grid
,这是 的结果网格调用您分配给按钮"?!
No, you're entirely wrong: your code does not show that ttk.Button
returns None
-- it shows that the grid
method on the button object returns None
! Don't you see that you're calling .grid
on whatever it is that ttk.Button
returns (the button object), and it's the result of that grid call that you're assigning to "button"?!
所以做正确的事...:
So do it right instead...:
button = ttk.Button(root, text="Test")
button.grid(row=0, column=0)
现在您可以打印按钮
,当然结果会大不相同!-)
now you can print button
and of course the results will be very different!-)
这篇关于ttk.Button 返回 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!