Python 3 WxPython Stopwatch Digital Clock Timer Example in GUI Desktop App Full Project For Beginners

Python 3 WxPython Stopwatch Digital Clock Timer Example in GUI Desktop App Full Project For Beginners

 

Welcome folks today in this blog post we will be building a stopwatch digital clock timer in gui desktop app in python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the below library using the pip command as shown below

 

pip install wxpython

 

After installing this library you need to make an app.py file and copy paste the following code

See also  Python 3 (Tweepy) Tkinter GUI Script to Make a Twitter API Bot Desktop App to Tweet,Follow Users and Retweet Full Project For Beginners

 

app.py

 

import wx
import time

class ClockWindow(wx.Window):
    def __init__(self, parent):
        wx.Window.__init__(self, parent)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.timer.Start(1000)

    def Draw(self, dc):
        t = time.localtime(time.time())
        st = time.strftime("%I:%M:%S", t)
        w, h = self.GetClientSize()
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.Clear()
        dc.SetFont(wx.Font(30, wx.SWISS, wx.NORMAL, wx.NORMAL))
        tw, th = dc.GetTextExtent(st)
        dc.DrawText(st, (w-tw)/2, (h)/2 - th/2)
        
    def OnTimer(self, evt):
        dc = wx.BufferedDC(wx.ClientDC(self))
        self.Draw(dc)

    def OnPaint(self, evt):
        dc = wx.BufferedPaintDC(self)
        self.Draw(dc)

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="wx.Timer")
        ClockWindow(self)
        

app = wx.PySimpleApp()
frm = MyFrame()
frm.Show()
app.MainLoop()

 

See also  Python 3 Tkinter Validating or Limiting Entry in Numerical Textbox Widget GUI Desktop App Full Project For Beginners

 

If you execute the above python script by typing the below command

 

python app.py

 

 

Leave a Reply