ttk.Button 返回 None

发布于 2024-08-17 04:28:07 字数 751 浏览 13 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ杯具 2024-08-24 04:28:07

不,你完全错了:你的代码显示ttk.Button返回None——它显示网格按钮对象上的 方法返回 None!您没有看到您正在对 ttk.Button 返回的任何内容(按钮对象)调用 .grid ,并且它是 的结果grid 调用您分配给“按钮”?!

因此,请正确执行...:

button = ttk.Button(root, text="Test")
button.grid(row=0, column=0)

现在您可以打印按钮,当然结果会非常不同!-)

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!-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文