Don't like ads? PRO users don't see any ads ;-)
Guest

basic gui with counter thread

By: waterapple on Jun 17th, 2012  |  syntax: Python  |  size: 3.23 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #-------------------------------------------------------------------------------
  2. # Name:        module1
  3. # Purpose:
  4. #
  5. # Author:      new
  6. #
  7. # Created:     13/06/2012
  8. # Copyright:   (c) new 2012
  9. # Licence:     <your licence>
  10. #-------------------------------------------------------------------------------
  11. #!/usr/bin/env python
  12.  
  13. import wx
  14. import os
  15. import time
  16. import threading
  17. import sys
  18.  
  19.  
  20.  
  21.  
  22. class Mainwindow(wx.Frame):
  23.     def __init__(self,parent,title):
  24.         wx.Frame.__init__(self,parent,title=title)
  25.         self.setupworker()
  26.         self.initialize()
  27.  
  28.     def initialize(self):
  29.         sizer = wx.GridBagSizer()
  30.  
  31.         self.entry = wx.TextCtrl(self,-1,value=u"Enter text here.")
  32.         sizer.Add(self.entry,(0,0),(1,1),wx.EXPAND)
  33.         self.Bind(wx.EVT_TEXT_ENTER, self.OnPressEnter, self.entry)
  34.  
  35.         button = wx.Button(self,-1,label="Click me !")
  36.         sizer.Add(button, (0,1))
  37.         self.Bind(wx.EVT_BUTTON, self.OnButtonClick, button)
  38.  
  39.         self.label = wx.StaticText(self,-1,label=u'Hello !')
  40.         self.label.SetBackgroundColour(wx.BLUE)
  41.         self.label.SetForegroundColour(wx.WHITE)
  42.         sizer.Add( self.label, (1,0),(1,2), wx.EXPAND )
  43.  
  44.         startbutton = wx.Button(self,-1,label="Start worker")
  45.         sizer.Add(startbutton, (3,1))
  46.         self.Bind(wx.EVT_BUTTON, self.startworker, startbutton)
  47.  
  48.         stopbutton = wx.Button(self,-1,label="Stop worker")
  49.         sizer.Add(stopbutton, (3,2))
  50.         self.Bind(wx.EVT_BUTTON, self.stopworker, stopbutton)
  51.  
  52.         sizer.AddGrowableCol(0)
  53.         self.SetSizerAndFit(sizer)
  54.         self.Center()
  55.         self.Show(True)
  56.     def OnButtonClick(self,event):
  57.         print "You clicked the button !"
  58.         self.label.SetLabel(self.entry.GetValue()+"You clicked the button")
  59.         self.entry.SetFocus()
  60.         self.entry.SetSelection(-1,-1)
  61.  
  62.     def OnPressEnter(self,event):
  63.         print "You pressed enter !"
  64.         self.label.SetLabel(self.entry.GetValue()+"You presses enter")
  65.         self.entry.SetFocus()
  66.         self.entry.SetSelection(-1,-1)
  67.  
  68.     def onabout(self,event):
  69.         dlg = wx.MessageDialog(self,"A small text editor","about simple editor")
  70.         dlg.ShowModal()
  71.         dlg.Destroy
  72.  
  73.     def onexit(self,event):
  74.         self.counter.stop()
  75.         self.Close(True)
  76.     def setupworker(self):
  77.         self.worker = counter()
  78.         self.worker.start()
  79.     def startworker(self,event):
  80.         self.worker.go()
  81.  
  82.     def stopworker(self,event):
  83.         self.worker.stop()
  84.  
  85.  
  86.  
  87.  
  88. class counter(threading.Thread):
  89.     def __init__(self):
  90.         threading.Thread.__init__(self)
  91.         self.running = False
  92.         self.counter = 0
  93.     def run(self):
  94.         while True:
  95.             time.sleep(.05)
  96.             while self.running:
  97.                 self.counter += 1
  98.                 print self.counter
  99.                 time.sleep(5)
  100.  
  101.     def go(self):
  102.         print "starting counter"
  103.         self.running = True
  104.     def stop(self):
  105.         print "stopping counter"
  106.         self.running = False
  107.     def getresults(self):
  108.         print self.counter
  109.         return self.counter
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. app = wx.App(False)
  121. frame = Mainwindow(None,"Main window Title")
  122.  
  123.  
  124. app.MainLoop()
  125. sys.exit()