restructure it to initialize widgets on startupcall mainloop once to start event processing and display the main windowand move all program logic into callback functions triggered by user actions your original program' actions become event handlersand your original main flow of control becomes program that builds main windowcalls the gui' event loop onceand waits this is the traditional way to structure gui programand it makes for coherent user experiencewindows pop up on requestinstead of showing up at seemingly random times until you're ready to bite the bullet and perform such structural conversionthoughthere are other possibilities for examplein the shellgui section earlier in this we saw how to add windows to file packing scripts to collect inputs (example - and beyond)laterwe also saw how to redirect such scriptsoutputs to guis with the guioutput class (example - this approach works if the non-gui operation we're wrapping up in gui is single operationfor more dynamic user interactionother techniques might be needed it' possiblefor instanceto launch gui windows from non-gui main programby calling the tkinter mainloop each time window must be displayed it' also possible to take more grandiose approach and add completely separate program for the gui portion of your application to wrap up our survey of gui programming techniqueslet' briefly explore each scheme popping up gui windows on demand if you just want to add simple gui user interaction to an existing non-gui script ( to select files to open or save)it is possible to do so by configuring widgets and calling mainloop from the non-gui main program when you need to interact with the user this essentially makes the program gui-capablebut without persistent main window the trick is that mainloop doesn' return until the gui main window is closed by the user (or quit method calls)so you cannot retrieve user inputs from the destroyed window' widgets after mainloop returns to work around thisall you have to do is be sure to save user inputs in python objectthe object lives on after the gui is destroyed example - shows one way to code this idea in python example - pp \gui\tools\mainloopdemo py ""demo running two distinct mainloop callseach returns after the main window is closedsave user results on python objectgui is goneguis normally configure widgets and then run just one mainloopand have all their logic in callbacksthis demo uses mainloop calls to implement two modal user interactions from non-gui main programit shows one way to add gui component to an existing non-gui scriptwithout restructuring code""from tkinter import from tkinter filedialog import askopenfilenameasksaveasfilename more ways to add guis to non-gui code