Does one create an event to notify parent window/GUI of something?

Vlastimil Brom vlastimil.brom at gmail.com
Sun Mar 12 16:47:25 EDT 2017


2017-03-12 13:14 GMT+01:00 Chris Green <cl at isbd.net>:
...
>
> This question relates to how one communicates between windows/GUIs.
>
> When the program starts theres a main GUI, class name abookgui.  If
> you want to add new entries or modify existing entries an edit GUI is
> started in a separate window, class abookeditgui.
>
> I need the abookeditgui window to be able to tell the parent abookgui
> window that it has completed whatever it was doing.
...
>
> --
> Chris Green
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list

Hi,
I am not sure, whether it counts as the "bad way" of doing it, you
mentined, but the most obvious approach seems to be passing a
reference to the parent object on creation of the child object (e.g.
dialog window), that way you can have event handlers in the child
class/gui, which can call methods of the parent class; it can then
handle e.g. saving the data.
Other than that, you can use specialised mechanisms like pubsub,
especially for more complex requirements of event handling.
I don't have experience with pygtk, but I believe, there might be
similar principles like in other gui toolkits.
You may find this more comprehensive explanation (refering to tkinter) useful:
http://stackoverflow.com/a/33650527

The concept of controller is be more flexible for some usecases,
however at the very basics you can couple the child class to the
parent directly, such as in the following sample:
http://stackoverflow.com/a/30690881

Note especially the referencing of objects between classes in the
mentioned code:

class Widgets(tk.Frame):
    def __init__(self, parent):
    ...
        self.parent = parent
  ...
        self.button = tk.Button(text='hello', command=self.parent.get_details)



...

class App(tk.Frame):
...
        self.widgets = Widgets(self)
  ...
   def get_details(self): ...


And of course, you could probably also use a global variable to
reference the respective objects, but this would most likely count as
the bad way to do it in some respect.

I believe, others might give more specific answers, with respect to
the gui you are using.

hth,
   vbr


More information about the Python-list mailing list