Refreshing code in Tkinter window with button

邮差的信 提交于 2019-12-13 04:50:44

问题


I'm 99% this isn't possible since python is first byte compiled then probably opens the Tk windows, but I'm wondering if there is any circumstance to add a button to refresh your tk app in place after you save the app its actually written in?

You can imagine after updating padding or some minor attribute it would be so cool to just hit the button to refresh the frame instead of closing and starting a new instance.

something...

class myapp()
 def __init___(self,root):
   self.root = root
   main_menu = ttk.Frame(self.root)
   ttk.Button(main_menu,text="Refresh",command=lambda root=self.root:refresh_me(root)))
 def refresh_me(self,root):
     #refresh the window I'm in somehow...
root = Tkinter.Tk()
myapp = myapp(root)
root.mainloop()

回答1:


this doesnt exactly answer the question but does provide a path to your stated goal

first easy_install q

then

class myapp()
 def __init___(self,root):
   self.root = root
   main_menu = ttk.Frame(self.root)
   ttk.Button(main_menu,text="Refresh",command=lambda root=self.root:refresh_me(root)))
 def shell_me(self,root):
     #refresh the window I'm in somehow...
     import q
     q.d()

at which point a shell opens and you can do something like

>>> myapp.padding = "10px" #or whatever you are trying to modify
>>> exit()

your app will then resume with the updated params.

there are other options like pycrust that open an interactive shell that may not block the main loop and probably include some additional functionality ... but q.d() is in my experience the easiest one to just toss up




回答2:


Wow I figured it out. I made two mains. One to import and the other to refresh.

Here you go:

#name of file is python_script.py

class myapp()
 def __init___(self,root):
   self.root = root
   main_menu = ttk.Frame(self.root)
   ttk.Button(main_menu,text="REFRESH",command=lambda self=self:self._update())

 def _update(self):
   import python_script
   python_script.main_refresh(self.root,python_script)

def main_refresh(root,python_script):
   reload(python_script)
   root.destroy()
   python_script.main()

def main():
   root = Tkinter.Tk()
   myapp = myapp(root)
   root.mainloop()

if __name__ == '__main__':
   main() 


来源:https://stackoverflow.com/questions/19825225/refreshing-code-in-tkinter-window-with-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!