Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python tkinter | after jump to parent class function how to back to subclass function?

There is the code.

class Popup(Menu):
    def __init__(self,parent):
        Menu.__init__(self, parent,tearoff=0)
        self.add_command(label='click',command=self.right_click)

    def popup(self,x,y):
        try:
            self.tk_popup(x,y)
        finally:
            self.grab_release()
    
    def right_click(self):
        print('Click!')


class main(Popup):
    def __init__(self) -> None:
        main_window=tkinter.Tk()
        self.label=tkinter.Label(main_window,text='test')
        self.label.pack()
        Popup.__init__(self,main_window)
        self.label.bind('<ButtonRelease-3>',lambda event: self.popup(event.x_root,event.y_root))
        main_window.mainloop()
    
    def click(self):
        self.label.config(text='click')


main()

I want to do main.click after Popup.right_click, but I have no idea how to back to subclass function?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can simply call self.click() inside right_click(). Suggest to define click() inside Popup as well in case click() is not defined inside subclass. click() in subclass will override click() in Popup class.

class Popup(Menu):
    ...

    def right_click(self):
        print('Click!')
        self.click()

    def click(self):
        pass
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading