Skip to main content
  1. About
  2. For Teams
Asked
Modified 7 months ago
Viewed 2k times
4

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

But for others, here's the explanation:

  • 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)

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.

16
  • 3
    the library would have had to implement this feature, as far as I know, they haven't, so this is not very practical to achieve, not impossible and I can think of a few solutions but all of them would require a lot of work, the easiest one I can think of would constantly open and close the window which you might as well do manually and only when you need
    Matiiss
    –  Matiiss
    2022-03-20 12:50:34 +00:00
    Commented Mar 20, 2022 at 12:50
  • @Matiiss , thanks for your response, it would be helpful you can draft am answer providing atleast the logic or some sort of hint on how to achive the solution.
    Faraaz Kurawle
    –  Faraaz Kurawle
    2022-03-21 13:04:58 +00:00
    Commented Mar 21, 2022 at 13:04
  • The solution to this would be very complicated. There's a difference between a web page and an actual running Python script. There's no way to change the execution of one version of a script into the execution of another version of the script without restarting the whole program. Maybe, at least, you could import most of your classes and what-not from another file, and then re-import it when the file is edited? Again, that would be extremely difficult. You'd probably be better off making a website.
    Sylvester is on codidact.com
    –  Sylvester is on codidact.com
    2022-03-24 23:48:07 +00:00
    Commented Mar 24, 2022 at 23:48
  • 2
    You would need to build a whole framework for this. Stack Overflow isnt designed to answer these types of questions.
    Thingamabobs
    –  Thingamabobs
    2022-03-26 08:45:58 +00:00
    Commented Mar 26, 2022 at 8:45
  • 1
    Can you clarify what you mean by "the whole window should be updated"? Is it ok if all of the data entered in the UI is reset to their default values?
    Bryan Oakley
    –  Bryan Oakley
    2022-03-28 16:49:13 +00:00
    Commented Mar 28, 2022 at 16:49

2 Answers 2

2
+50

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!

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the solution it works perfect, but would it be possible to update just a particular frame?
0

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.

15 Comments

As I understand, it only works with 1 GUI, and you need to name it root?
@D_00 , it executes the code in the selected region, so if you have multiple GUI under selected region you will notice changes in them too, and it's not necessary to name it root, you can change the name according to your wish.
Oh, I did not understand quite right. Also, do you need to manually type in "@start@" and "@end@" in the file? I haven't used file.find() before.
@D_00, Yes, file.find() isnt a function of file, its a function on string, it finds the index of the string given in it.
A nice thing would be to do this automatically, for example with the use of the difflib module, but then it tends to be complicated. I used it in this code on GitHub, you can check it out if you want, but it's not completely finished.
|

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.