Search code examples
pythoneventstkinterttk

Refreshing code in Tkinter window with button


I'm 99% this isn't possible since python is first byte compiled then probably opens the Tk windows, but I'm wondering if there is any circumstance to add a button to refresh your tk app in place after you save the app its actually written in?

You can imagine after updating padding or some minor attribute it would be so cool to just hit the button to refresh the frame instead of closing and starting a new instance.

something...

class myapp()
 def __init___(self,root):
   self.root = root
   main_menu = ttk.Frame(self.root)
   ttk.Button(main_menu,text="Refresh",command=lambda root=self.root:refresh_me(root)))
 def refresh_me(self,root):
     #refresh the window I'm in somehow...
root = Tkinter.Tk()
myapp = myapp(root)
root.mainloop()

Solution

  • Wow I figured it out. I made two mains. One to import and the other to refresh.

    Here you go:

    #name of file is python_script.py
    
    class myapp()
     def __init___(self,root):
       self.root = root
       main_menu = ttk.Frame(self.root)
       ttk.Button(main_menu,text="REFRESH",command=lambda self=self:self._update())
    
     def _update(self):
       import python_script
       python_script.main_refresh(self.root,python_script)
    
    def main_refresh(root,python_script):
       reload(python_script)
       root.destroy()
       python_script.main()
    
    def main():
       root = Tkinter.Tk()
       myapp = myapp(root)
       root.mainloop()
    
    if __name__ == '__main__':
       main()