Reload Tkinter application after code change Reload Tkinter application after code change tkinter tkinter

Reload Tkinter application after code change


This question has been asked a number of times

Proper way to reload a python module from the console

You can use reload(module) for this, but beware of nasty side effects. For example, existing code will be based on the original code, it will not magically get new attributes or baseclasses added.

Another great resource on this would be https://code.activestate.com/recipes/580707-reload-tkinter-application-like-a-browser/

After you've described you're problem a bit further in the comments, I was able to reconstruct the issue better on my end.

Here's my basic implementation for a solution to your issue:

from tkinter import *import jsonapplication_state = {"background": "white"}  background_colors = ["white", "red", "blue", "yellow", "green"]def main():   root = Tk()   root.title("Test")   root.geometry("400x400")   reload_state(root)   for color in background_colors:      def change_background_wrapper(color=color, root=root):         change_background(color, root)      Button(root, text=color,command=change_background_wrapper).pack()   root.mainloop()def change_background(color, window):   print("new color: " + color)   window.configure(bg=color)   application_state["background"] = color   update_config()def reload_state(window):   config_file = open("config.json")   conf = json.load(config_file)   window.configure(bg=conf["background"])def update_config():   with open("config.json", "w") as conf:      conf.write(json.dumps(application_state))if __name__ == "__main__":   main()

In this instance, we're able to update the background color of the GUI and it will persist on each rerun of the script, until it gets manually changed again. You can apply this concept to pretty much any sort of state you have inside your application, I think this should get you started!