How to use stop watch (Phoenix)

Keywords : StopWatch, Measure time intervals, Semaphore, Time elapsed.


Introduction

Many GUI programs (especially games) feature a stopwatch to track time.

This snippet demonstrates the basics of how to use a wx.StopWatch in an application.


What Objects are Involved

Need I say more? :)


Process Overview

Once launched, this recipe shows a window titled "Hello World!", which consists of four buttons and a status bar.

The status bar initially reads "Ready" and changes its message with each click of a button.

A semaphore is used to keep track of the stopwatch mode.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Tested py2.6.2, wx2.8.10 and WinXP SP3 (StevenSproat)

Tested py2.4, wx2.5.3.1 and WinXP SP2 (Tian Xie)

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Code sample

img_sample_one.png

ICON file : wxwin.ico

   1 # sample_one.py
   2 
   3 import wx
   4 
   5 # class MyGui
   6 
   7 #---------------------------------------------------------------------------
   8 
   9 class MyGui(wx.Frame):
  10     """
  11     Hello World!
  12     Once stopped, a typical stopwatch reports total time elapsed since
  13     started. A stopwatch can be paused after started and time won't be
  14     recorded until it's resumed. A paused stopwatch must be resumed.
  15     """
  16     def __init__(self, parent, title):
  17         # Create a non-resizable frame.
  18         wx.Frame.__init__(self, parent, title=title,
  19                           style=wx.DEFAULT_FRAME_STYLE |
  20                                 wx.RESIZE_BORDER)
  21 
  22         self.SetIcon(wx.Icon('wxwin.ico'))
  23 
  24         # Simulate a stopwatch with four buttons.
  25         StartButton = wx.Button(self, -1, "Start")
  26         PauseButton = wx.Button(self, -1, "Pause")
  27         ResumButton = wx.Button(self, -1, "Resume")
  28         Stop_Button = wx.Button(self, -1, "Stop!")
  29 
  30         # A status bar displays current stopwatch mode.
  31         self.CreateStatusBar()
  32         self.SetStatusText("Ready.")
  33 
  34         # Put more GUI code here for a fancier application.
  35 
  36         # Use a plain sizer for the layout.
  37         MyBS = wx.BoxSizer(wx.HORIZONTAL)
  38         # Each non-resizable button has roomy borders (10 pixels).
  39         MyBS.Add(StartButton, 0, wx.ALL, 10)
  40         MyBS.Add(PauseButton, 0, wx.ALL, 10)
  41         MyBS.Add(ResumButton, 0, wx.ALL, 10)
  42         MyBS.Add(Stop_Button, 0, wx.ALL, 10)
  43 
  44         self.SetSizerAndFit(MyBS)
  45 
  46         # Create a stopwatch.
  47         self.MySW = wx.StopWatch()
  48         # Set its initial mode to be "stopped".
  49         self.semaphore = 0
  50 
  51         self.Bind(wx.EVT_BUTTON, self.onStart, StartButton)
  52         self.Bind(wx.EVT_BUTTON, self.onPause, PauseButton)
  53         self.Bind(wx.EVT_BUTTON, self.onResum, ResumButton)
  54         self.Bind(wx.EVT_BUTTON, self.onStop_, Stop_Button)
  55 
  56     #-----------------------------------------------------------------------
  57 
  58     def onStart(self, evt):
  59         if self.semaphore == 0:
  60             self.MySW.Start()
  61             self.semaphore = 2 # Set the mode to be "started".
  62             message = "Go!"
  63         else:
  64             message = "Hold. I've been started already."
  65         self.SetStatusText(message)
  66 
  67     def onPause(self, evt):
  68         if self.semaphore > 0: # A stopwatch was resumed or started.
  69             self.MySW.Pause()
  70             self.semaphore = -1 # Set the mode to be "paused".
  71             message = "Freeze!"
  72         else:
  73             message = "I cannot pause because I'm still."
  74         self.SetStatusText(message)
  75 
  76     def onResum(self, evt):
  77         if self.semaphore == -1:
  78             self.MySW.Resume()
  79             self.semaphore = 1 # Set the mode to be "resumed".
  80             message = "I'm ticking again."
  81         else:
  82             message = "I cannot resume because I'm not paused."
  83         self.SetStatusText(message)
  84 
  85     def onStop_(self, evt):
  86         if self.semaphore > 0:
  87             message = str(self.MySW.Time()) + " milliseconds recorded."
  88             self.semaphore = 0
  89         else:
  90             message = "I cannot stop because I'm still."
  91         self.SetStatusText(message)
  92 
  93 #---------------------------------------------------------------------------
  94 
  95 app = wx.App()
  96 frame = MyGui(None, "Stop watch")
  97 frame.Show()
  98 app.MainLoop()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Tian Xie (sample_one.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

03/08/05 - Tian Xie (Author).

21/12/19 - Ecco (Updated page for wxPython Phoenix).


Comments

- Please test this program on other platforms or versions. If it doesn't work, find a fix and correct it here. Thanks.

I wonder how to implement a stopwatch without a semaphore. -- Tian Xie

- blah, blah, blah....

How to use stop watch (Phoenix) (last edited 2020-12-13 17:30:42 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.