Friday, 25 March 2022

Applying LiveServer Logic for Tkinter File

The people who are familiar with the Live Server of VS Code, would have easily understood what is the main motive of this question.

But for others, here's the explanation:

  • Main motive of Live Server is to Automatically Reload Your Site on Save in web development! (Which get changed for python tkinter).
  • When ever I change something in my python file which contains tkinter code, the change should be reflected in the main window (the main window should not re-open to reflect the changes).

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?:

  • I tried to detect change in file using os.getsize which satisfied the first part of my question, but however I am not able to solve the second part i.e window should not be closed.
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)


from Applying LiveServer Logic for Tkinter File

No comments:

Post a Comment