在 Tkinter 中实现热重载功能
熟悉React和Flutter的人很容易理解这一点。
但对于其他人来说,解释如下:
- 当我更改包含 tkinter 代码的 python 文件中的某些内容时,更改应该反映在主窗口中(主窗口不应重新打开以反映更改)。
我尝试在网络上以及堆栈溢出流上进行搜索,但所有结果都是为了更新条目、标签、按钮等中的值。但我想要的是,当我更改我的内容时,整个窗口都应该更新main 文件,并且不应重新打开主窗口来执行此操作。简而言之,在主文件中的每次更改时更新整个窗口而不关闭它,或者在保存时自动重新加载程序而不重新打开!
我尝试过什么?:
- 我尝试使用<检测文件中的更改code>os.getsize 满足了我问题的第一部分,但是我无法解决第二部分,即窗口不应关闭。
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 tkinter import *
root=Tk()
root.mainloop
如果我在 root=Tk() 之后添加了
,它应该向我显示标签,如果我删除了相同的代码,它应该删除它们。a=Label(text='text')
和 a.pack()
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尽我所能来回答你的问题,
我对具有热重载功能的flutter有一些(我自己的一些项目,仍然太有限)经验(与你上面描述的相同 ,你想要用Python,主要是
tkinter
),我最近切换到Python for gui(喜欢它!),所以我想在这里分享我的研究:我成功地能够设置热-用 kivy 重新加载两者(kivymd热重载,它带有看门狗和 kaki,可以实时工作),还有 tkinter,虽然后者有问题,但你必须按 Ctrl + R 来重新加载 tkinter 窗口,但它无需重新运行 python 程序即可工作,我将在这里留下找到的资源的链接,希望它有所帮助与您的查询!
要使用 tkinter 设置热重载(需要 Ctrl + R),请参阅 这里。
要使用我个人更喜欢的 kivy/kivymd (实时)设置热重载,您可以找到官方文档 此处。
值得一提的是,我在 Manjaro (Arch linux) 上使用了上面的内容和 pycharm、atom,但我也尝试过并使其在 Windows 10 上使用 vs code 成功运行(像 charm 一样工作)
希望我能有所帮助!如果您遇到任何相关问题,请随时询问!谢谢!
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!
经过深入研究,我终于找到了一种实现热重载功能的方法(@Stange 答案提供了),但只是更新所选的框架或代码。
基本思想是不断读取文件并执行所选代码,并删除列表中要删除的对象。
在这里,我不断检查是否按下了 ctrl+r 键,如果按下,
@Start@
和@End@
更新的代码的开始和结束。在主文件中,我添加了上面的代码,该代码不断检查 exec_log.txt 文件是否有任何更改,如果有更改,则执行它们,所有这些都会破坏在删除_列表。
这只是一个临时解决方案,就我而言,它可以帮助我在 tkinter 中实现热重载功能。
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.
Here I am constantly checking if if
ctrl+r
key is pressed, and if pressed@Start@
and@End@
respectively.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.