Faraaz Kurawle
Faraaz Kurawle

Reputation: 1182

Implementing Hot Reload feature in Tkinter

The people who are familiar with React and Flutter would easily understand this.

But for others, here's the explanation:

I have tried to search on web as well as on stack over flow, but all the results are for updating value in entry, label, buttons etc. But what I want is, the whole window should be updated when ever I change something in my main file, and the main window should not be reopened to do so. So in short, updating whole window without closing it, on every changes in the main file or automatically reload your program on save without reopening!

What have I tried?:

import os
main__tkinter_filename="myfile.py"
initial_filesize=os.path.getsize(main_tkinter_filename) # Getting size of the file for
                                                        # comparison.
while 1:
    final_filesize=os.path.getsize(main_tkinter_filename)
    if final_filsize<intial_filesize or final_filesize>initial_filesize:
        webbrowser.open(main_tkinter_filename)

Example:

from tkinter import *

root=Tk()
root.mainloop

results in the below GUI: Tkinter Basic GUI

If i have added a=Label(text='text')anda.pack() after root=Tk(), it should show me the label, and if i have removed the same code, it should remove them.

EDIT: I have invested a lot of time into this problem initially and have gained a significant, effective but highly complicated way to solve this problem. here's the repo.

Note: the code in repo is not in working state and would most probably never be in working state. The repo contains my idea to solve this problem as well as my work towards implementing the idea.

Upvotes: 3

Views: 1794

Answers (2)

Faraaz Kurawle
Faraaz Kurawle

Reputation: 1182

After digging around I have finally found out a way to implement hot reload feature (which @Stange answers provides) but just updating the selected frame or code.

The basic idea is constanly reading the file and executing the selected code, and removing the object in a list which are meant to be removed.

# Live Checker.py
import keyboard
while 1:
    if keyboard.is_pressed("Ctrl+r"):
        with open('test.py','r') as file:
            file_data=file.read()
            file_data_start_index=file_data.find("'@Start@'")
            file_data_end_index=file_data.find("'@End@'")
            exec_command=file_data[file_data_start_index:file_data_end_index]
            with open('exec_log.txt','w') as txt_file:
                txt_file.write(exec_command)

Here I am constantly checking if if ctrl+r key is pressed, and if pressed

  • it reads the file,
  • writes the selected code from the file into a txt file.
  • I have specified the start and end of the code to be updated by @Start@ and @End@ respectively.
# Main.py
def check():
    with open('exec_log.txt','r') as exec_c:
        exec_command=exec_c.read()

    if len(exec_command)==0:
        pass
    else:
        print(exec_command)
        exec('for i in root.winfo_children():i.destroy()\n'+exec_command)
        print('exec')
        with open('exec_log.txt','w') as exec_c:
            pass
        root.update()
    root.after(100,check)
root.after(100,check)

And in the main file, i have added the above code which continusly check if exec_log.txt file has any changes, and if changes are there, then it executes them and all so destroys the widget specified in the remove_list.

This is just a temporary solution which in my case helps me to implement the hot reload feature in tkinter.

Upvotes: 0

Strange
Strange

Reputation: 309

I will answer your question by the best of my understanding,

I have some (a few projects of my own, still way too limited) experience with flutter which has hot-reload feature (same as you described above, which you want with python, mainly tkinter), I recently switched to python for gui (Loved it!), so I would like to share my research here:

I was successfully able to set up hot-reload both with kivy (kivymd hot reload, which comes with watchdog and kaki, which works real-time), and with tkinter, while there is a hitch with the later, you will have to press Ctrl + R as to reload the tkinter window, but it works without having to re-run the python program, I will leave the link to the found resources here, hope it helps with your query!

To setup hot-reload with tkinter (requires Ctrl + R), please refer here.

To setup hot-reload with kivy/kivymd (real-time), which I personally prefer, you can find the official docs here.

To mention, I use the above on Manjaro (Arch linux) with pycharm, atom, but I have also tried and have made it run successfully on Windows 10 with vs code (worked like charm)

Hope I could be of help! If you face any problem regarding the same, please feel free to ask! Thanks!

Upvotes: 1

Related Questions