python - Run an infinite loop in the backgroung in Tkinter -


i code run in background , update gui periodically. how can accomplish this?

for example, suppose want execute in background of gui code can see below:

x = 0  while true:    print(x)    x = x + 1    time.sleep(1) 

this gui code:

class guiframework(frame):      def __init__(self,master=none):         frame.__init__(self,master)         self.master.title("volume monitor")         self.grid(padx=10, pady=10,sticky=n+s+e+w)         self.createwidgets()      def createwidgets(self):         textone = entry(self, width=2)         textone.grid(row=1, column=0)          listbox = listbox(self,relief=sunken)         listbox.grid(row=5,rowspan=2,column=0,columnspan=4,sticky=n+w+s+e,pady=5)         listbox.insert(end,"this alert message.")  if __name__ == "__main__":     guiframe = guiframework()     guiframe.mainloop() 

it little unclear code @ top supposed do, however, if want call function every second (or every amount of seconds want), can use after method.

so, if want textone, you'd like:

... textone = entry(self, width=2) textone.x = 0  def increment_textone():     textone.x += 1      # register "increment_textone" called every 1 sec     self.after(1000, increment_textone)  

you make function method of class (in case called callback), , code this:

class foo(frame):      def __init__(self, master=none):         frame.__init__(self, master)         self.x = 0         self.id = self.after(1000, self.callback)      def callback(self):         self.x += 1         print(self.x)         #you can cancel call doing "self.after_cancel(self.id)"         self.id = self.after(1000, self.callback)    gui = foo() gui.mainloop() 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -