简体   繁体   中英

Python, Tkinter: How to get a widget to run function on pack?

Say I have an instance of a custom class, CustomFrame , which inherits from Tkinter's Frame widget asssigned to custom_frame , a child of a root parent Tk window widget, and I pack (or place or grid) it:

custom_frame = CustomFrame(root)
custom_frame.pack()

Is there a way to make the custom_frame run a method of CustomFrame when it is packed, without just calling the function before packing custom_frame ? For example, somehow overriding the usual pack method to run the usual pack method and then call another method?

Since you are defining the frame anyway, just add this to your frame:

def pack(self, *args, **kwargs):
    super(CustomFrame, self).pack(*args, **kwargs)
    # Do whatever

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM