Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: iso-8859-1 -*-
- # safe_threading_demo.py
- import Tkinter,threading,time
- class MyProcess(threading.Thread):
- def __init__(self,startValue):
- threading.Thread.__init__(self)
- self._stop = False
- self._value = startValue
- def run(self):
- while self._value>0 and not self._stop:
- self._value = self._value - 1
- print u"Thread: I'm working... (value=%d)" % self._value
- time.sleep(1)
- print u"Thread: I have finished."
- def stop(self):
- self._stop = True
- def result(self):
- return self._value
- class MyGUI(Tkinter.Tk):
- def __init__(self,parent):
- Tkinter.Tk.__init__(self,parent)
- self.parent = parent
- self.initialize()
- self.worker = MyProcess(15)
- self.worker.start() # Start the worker thread
- def initialize(self):
- ''' Create the GUI. '''
- self.grid()
- button = Tkinter.Button(self,text=u"Click Me To Stop",command=self.OnButtonClick)
- button.grid(column=1,row=0)
- self.labelVariable = Tkinter.StringVar()
- label = Tkinter.Label(self,textvariable=self.labelVariable)
- label.grid(column=0,row=0)
- self.labelVariable.set(u"Hello... ")
- def OnButtonClick(self):
- '''' Called when button is clicked. '''
- self.labelVariable.set( u"Button clicked" )
- self.worker.stop() # We ask the worker to stop (it may not stop immediately)
- while self.worker.isAlive(): # We wait for the worker to stop.
- time.sleep(0.2)
- # We display the result:
- self.labelVariable.set( u"Result: %d " % self.worker.result() )
- if __name__ == "__main__":
- app = MyGUI(None)
- app.title('My Application')
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement