Class and tkinter problem
Peter Otten
__peter__ at web.de
Thu Jan 7 11:02:21 EST 2021
On 07/01/2021 08:42, Christian Gollwitzer wrote:
> Am 07.01.21 um 08:29 schrieb Paulo da Silva:
>
>> Does anybody know why cmd method isn't called when I change the button
>> state (clicking on it) in this example?
>> I know that this seems a weird class use. But why doesn't it work?
>> Thanks.
>>
>> class C:
>> from tkinter import Checkbutton
>> import tkinter
>>
>> @staticmethod
> ^^it works if you remove the staticmethod here
>
>> def cmd():
>> print("Test")
>>
>
> Maybe there is a bug in tkinter, that it doesn't work with static methods?
It has nothing to do with tkinter, a staticmethod object cannot be
called directly:
>>> class C:
@staticmethod
def cmd(): print("Hello")
cmd()
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
class C:
File "<pyshell#28>", line 4, in C
cmd()
TypeError: 'staticmethod' object is not callable
You have to go through the descriptor protocol:
>>> class C:
@staticmethod
def cmd(): print("Hello")
>>> C.cmd()
Hello
I recommend that the OP use a more conventional stye and do the setup
outside the class or, better, in an instance of the class.
More information about the Python-list
mailing list