Search code examples
python-3.xwxpython

implement ProgressDialog with Gauge


hello I want to implement my own ProgressDialog with a Gauge. I want to display the elapsed time.

Another thing. Is it possible to modify the Loader so that it faces round trips instead of going round in circles?

withPython 3.5.2

import wx
import time
class Example(wx.Frame):
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)       
        self.Bind(wx.EVT_TIMER, self.OnTimer)
        self.gspeed = 10
        self.timer = wx.Timer(self)
        self.timer.Start(self.gspeed)        
        self.star = True       
        self.InitUI()

    def InitUI(self):
        """ GUI
        """
        pnl = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)      
        self.btn1 = wx.Button(pnl, wx.ID_OK)
        self.btn2 = wx.Button(pnl, wx.ID_STOP)
        self.text = wx.StaticText(pnl)
        self.count = wx.StaticText(pnl)
        self.Bind(wx.EVT_BUTTON, self.OnOk, self.btn1)
        self.Bind(wx.EVT_BUTTON, self.OnStop, self.btn2)
        self.gauge = wx.Gauge(pnl, size=(250, -1), style = wx.GA_HORIZONTAL)
        hbox1.Add(self.gauge, proportion=1, flag=wx.ALIGN_CENTRE)        
        hbox2.Add(self.btn1, proportion=1, flag=wx.RIGHT, border=10)
        hbox2.Add(self.btn2, proportion=1)
        hbox3.Add(self.text, proportion=1, flag=wx.RIGHT, border=50)
        hbox3.Add(self.count, proportion=1)
        vbox.Add((0, 30))
        vbox.Add(hbox1, flag=wx.ALIGN_CENTRE)
        vbox.Add((0, 20))
        vbox.Add(hbox2, proportion=1, flag=wx.ALIGN_CENTRE)
        vbox.Add(hbox3, proportion=1, flag=wx.ALIGN_CENTRE)
        pnl.SetSizer(vbox)
        self.SetTimeLabel()        
        self.SetTitle('SDA')
        self.Centre()        
    def OnOk(self, e):        
        self.text.SetLabel('Elapsed time')
        self.count.SetLabel("0")

    def OnStop(self, e):
        self.timer.Stop()
        self.text.SetLabel('Task Interrupted')
    def OnTimer(self, e):
        self.gauge.Pulse()        
    def SetTimeLabel(self):
        self.text.SetLabel('Task')        
def main():
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
if __name__ == '__main__':
    main() 

my priority is the time elapsed. Thank you


Solution

  • To get the elapsed time subtract the start time from the current time. I've added the method get_elapsed_time to your example below.

    import wx
    import time
    
    
    class Example(wx.Frame):
        def __init__(self, *args, **kw):
            super(Example, self).__init__(*args, **kw)
            self.Bind(wx.EVT_TIMER, self.OnTimer)
            self.gspeed = 10
            self.timer = wx.Timer(self)
            self.timer.Start(self.gspeed)
            self.star = True
    
            self.start_time = time.time()
    
            self.InitUI()
    
        def InitUI(self):
            """ GUI
            """
            pnl = wx.Panel(self)
            vbox = wx.BoxSizer(wx.VERTICAL)
            hbox1 = wx.BoxSizer(wx.HORIZONTAL)
            hbox2 = wx.BoxSizer(wx.HORIZONTAL)
            hbox3 = wx.BoxSizer(wx.HORIZONTAL)
            self.btn1 = wx.Button(pnl, wx.ID_OK)
            self.btn2 = wx.Button(pnl, wx.ID_STOP)
            self.text = wx.StaticText(pnl)
            # self.count = wx.StaticText(pnl)
            self.Bind(wx.EVT_BUTTON, self.OnOk, self.btn1)
            self.Bind(wx.EVT_BUTTON, self.OnStop, self.btn2)
            self.gauge = wx.Gauge(pnl, size=(250, -1), style=wx.GA_HORIZONTAL)
            hbox1.Add(self.gauge, proportion=1, flag=wx.ALIGN_CENTRE)
            hbox2.Add(self.btn1, proportion=1, flag=wx.RIGHT, border=10)
            hbox2.Add(self.btn2, proportion=1)
            hbox3.Add(self.text, proportion=1, flag=wx.RIGHT, border=50)
            # hbox3.Add(self.count, proportion=1)
            vbox.Add((0, 30))
            vbox.Add(hbox1, flag=wx.ALIGN_CENTRE)
            vbox.Add((0, 20))
            vbox.Add(hbox2, proportion=1, flag=wx.ALIGN_CENTRE)
            vbox.Add(hbox3, proportion=1, flag=wx.ALIGN_CENTRE)
            pnl.SetSizer(vbox)
            self.SetTimeLabel()
            self.SetTitle('SDA')
            self.Centre()
    
            self.Layout()
    
        def OnOk(self, e):
            self.SetTimeLabel()
            # self.count.SetLabel("0")
    
        def OnStop(self, e):
            self.timer.Stop()
            self.text.SetLabel('Task Interrupted')
    
        def OnTimer(self, e):
            self.gauge.Pulse()
            self.SetTimeLabel()
    
        def get_elapsed_time(self):
            return round(time.time() - self.start_time, 1)
    
        def SetTimeLabel(self):
            self.text.SetLabel("{elapsed} seconds elapsed".format(elapsed=self.get_elapsed_time()))
    
    
    def main():
        app = wx.App()
        ex = Example(None)
        ex.Show()
        app.MainLoop()
    
    
    if __name__ == '__main__':
        main()