Class and tkinter problem

Paulo da Silva p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt
Thu Jan 7 12:38:04 EST 2021


Às 07:29 de 07/01/21, Paulo da Silva escreveu:
> Hi!
> 
> 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
>     def cmd():
>         print("Test")
> 
>     top=tkinter.Tk()
>     cb=Checkbutton(command=cmd)
>     cb.pack()
> 
>     @staticmethod
>     def loop():
>         C.top.mainloop()
> 
> c=C()
> c.loop()
> 

After some experiments I found two solutions:

1)

class C:
    @staticmethod
    def cmd():
        print("Test")
class C: #@DuplicatedSignature
    from tkinter import Checkbutton
    import tkinter

    top=tkinter.Tk()
    cb=Checkbutton(command=C.cmd)
    cb.pack()

    @staticmethod
    def loop():
        C.top.mainloop()

c=C()
c.loop()

2)

class C:
    from tkinter import Checkbutton
    import tkinter

    @staticmethod
    def cmd():
        print("Test")

    top=tkinter.Tk()
    cb=Checkbutton(command=lambda : C.cmd())
    cb.pack()

    @staticmethod
    def loop():
        C.top.mainloop()

c=C()
c.loop()

This one as a sequence of the answer of Terry - thanks.

BTW, does anyone know I can I get the arguments eventually passed by the
Checkbutton event, or any other widget callback (*pargs, **kargs) using
this "solution"?


More information about the Python-list mailing list