ttk.Button returns None

南楼画角 提交于 2019-12-17 14:53:42

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/2034576/ttk-button-returns-none

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!