ttk.Button 返回 None
我正在尝试使用 ttk.Button 的调用方法,如 TkDocs (查看“命令回调”),但我不断收到此错误:
AttributeError:“NoneType”对象没有属性“invoke”
因此,我在交互式 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。
ttk.Button 是否意味着返回 None 。如果是这样,为什么 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' object has no attribute 'invoke'
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
Which shows that ttk.Button returns None.
Is ttk.Button meant to return None. And, if so, why does TkDocs say that there is an invoke method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,你完全错了:你的代码不显示
ttk.Button
返回None
——它显示网格按钮对象上的
方法返回None
!您没有看到您正在对ttk.Button
返回的任何内容(按钮对象)调用.grid
,并且它是 的结果grid 调用您分配给“按钮”?!因此,请正确执行...:
现在您可以
打印按钮
,当然结果会非常不同!-)No, you're entirely wrong: your code does not show that
ttk.Button
returnsNone
-- it shows that thegrid
method on the button object returnsNone
! Don't you see that you're calling.grid
on whatever it is thatttk.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...:
now you can
print button
and of course the results will be very different!-)