0

I have an tkinter application that can can open child forms when I click a button.

def open_new_form():
   child_form = ChildForm()
   list_of_children_form.append(child_form)
   child_form.show()

How do I keep track of all the forms that was created by the main app ?

I would like to be able to count the number of child forms that are active (not closed). I can use a counter that increments every time I create a new child form , but how do I update the counter if a child form is closed/destroyed.

Update 1: I have a list to record the created child forms (see updated code above) . How do I track the state (closed or not) of those in the list?

What I wanted to do:

  1. My main window opens 4 child_forms (c1, c2, c3, c4 )
  2. It created a list of form [c1, c2, c3, c4]
  3. I closed child_form c1, how can I inform the main window that I closed c1 so that it can remove it from the list.

Update 2: Tried suggestion from [furas]. I added a method delete_child() on the main form, passed the parent form to the child form.

def __init__(self, parent):
        self._parent = parent

def _on_form_closing(self):
        self._parent.delete_child()
        self.child_form.destroy()

However, i get this error :

AttributeError: '_tkinter.tkapp' object has no attribute 'delete_child'
trashvin
  • 545
  • 1
  • 5
  • 14
  • Have you tried simply appending them to a list? – Bryan Oakley Jul 13 '19 at 05:23
  • Yup. I added them to a list. The question is, how to track if those forms has been closed by the user? – trashvin Jul 13 '19 at 05:25
  • Can't you just remove that form from the list upon exit? – Henry Yik Jul 13 '19 at 05:57
  • So here is the scenario : I opened 5 child_forms , my list records it. Now I closed the 2 created child_form, does it send a callback to the main window to indicate that those 2 forms were closed so that my main window can update the list of active forms to only 3 [5 - 2 ]? – trashvin Jul 13 '19 at 06:25
  • You can take a look at [this post](https://stackoverflow.com/questions/111155/how-do-i-handle-the-window-close-event-in-tkinter) which describes how to bind a function to window close event. – Henry Yik Jul 13 '19 at 06:40
  • I have a binding already to the close event on the child form, what do I need to add to its form close event to signal the main window that it is closing? in this way main window can update the list? – trashvin Jul 13 '19 at 06:52
  • you can send main window as parameter to child form and then it can execute function which exists in main window or access elements in main window like `list_of_children_form` and remove itself from this list. – furas Jul 13 '19 at 08:45
  • tried passing the parent form as parameter, invoked the parent method to update the list on close but i got error (see updated post) – trashvin Jul 17 '19 at 13:46

0 Answers0