Code flow question due to unexpected code flow

I have a situation where I have a main window with a menu.

When a menu element is selected, a window comes up with a Treeview.

After the tree view/window has been destroyed, It then calls another function which is to launch a win which is then populated by a sqLite database, based on the returned value of the Treeview window.

(I don’t know if this is a top level issue or not)

So its Window, Menu → a command is executed calling a function which calls a window and

Win = Tk()

Menucreated and populated …

fromCommandOfMenu → menu item selected and command function called → doSomeThing


def doSomeThing()

X =- newWindow(Treeview)

(… then newWindow closes is returned)

Y = callNextNewWindow(x)

Then, return to the main menu

However, the resulting flow is quite different.

The menu becomes available, the command on the men elemtent is executed and the Window with the treeview is presented, but when the treeview/window closes (is destroyed), the control reverts back to the main window.

When the main window is then close, the function Y = callNewWindow(x) then gets executed. I do not understand why the workflow is acting in this way. It seems that the call from the function returns to the menu instead of executing the next function call to another menu.

I have many other instances where calling another window from within an existing window works without problem, but this does not. Cascading from one window through to another seems to be where the issue resides. If anyone has any thoughts or advice I would be most grateful.

I can broadly think of alternative, but cannot think if why this should not work and feel that there has to be a better solution…

I apologise if this come across a little cryptic. I am fairly new and not too bright so I would appreciate any input that could be offer from somehow more skilled or more wise than I

Without seeing some code that demonstrates the issue, it becomes somewhat difficult to guide you in the right direction.

It sounds like a logic-flow fail somewhere.

No, that is not how it works. The Tk is a class that has 2 parts to it. It is an application, which holds a main window. The mainloop is part of the application and is active throughout the lifetime of your application.

When the second window (TopLevel) is opened, it still uses the mainloop, it is never exited until you want to exit your program. This loop gets the actions you take (like closing your new window) and calls the so-called callbacks that execute the actions that need to be taken (remove the window from screen, give focus back to the main window etc.). If you put code after the mainloop call, it is cleaning up after you finish your application, so that is why it opens the window afterwards.

>>> def main():
...    root = tk.Tk()
...    print("After create root; window shows")
...    tk.mainloop()
...    print("After mainwindow close")
>>> import tkinter as tk
>>> main()
After create root; window shows
After mainwindow close

Try this, the main window closes and you will see that only than “After mainwindow close” is printed.

Thank you greatly. I very much appreciate your feedback. I don’t know if I’m smart/skilled or stupid (I fear the latter I regret), but i have found a work around which seems to work equally well. If I just .close() the form on which my grid exists, and then destroy it, control is returned to the next line in the calling function as opposed to the return to the main window. I have little doubt that this is not good/great, but it works. If you could think of any implication/unintended consequences of this, I would be ever so grateful. Thanks again for your explanation. It has helped me greatly. I would happily refactor it to get this to work as you have noted should that be warranted. I am deeply honoured that you took the time to look at my post. Thank you.