为什么在使用Wx.Timer和Time Module时会出现不同的时间?

时间:2013-11-22 01:36:36

标签: python time

我正在尝试创建一个计时器,我已经看到一些使用Wx.Timer或时间模块计算两个事件之间经过时间的示例(按开始按钮/按停止按钮)。 我正在使用以下代码,我想知道我得到的值是否正确或我做错了什么。

class MyFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer2 = wx.BoxSizer( wx.VERTICAL )

        bSizer4 = wx.BoxSizer( wx.VERTICAL )
        self.staticText1_value = 0
        self.staticText4_value_2 = 0
        bSizer7 = wx.BoxSizer( wx.HORIZONTAL )

        bSizer11 = wx.BoxSizer( wx.HORIZONTAL )

        self.m_staticText2 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Wx.Timer", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText2.Wrap( -1 )
        self.m_staticText2.SetFont( wx.Font( 20, 70, 90, 90, False, wx.EmptyString ) )

        bSizer11.Add( self.m_staticText2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL|wx.EXPAND|wx.ALIGN_BOTTOM, 5 )

        bSizer7.Add( bSizer11, 1, wx.EXPAND, 5 )

        bSizer12 = wx.BoxSizer( wx.VERTICAL )

        self.m_staticText1 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"0", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.timer = wx.Timer(self)
        self.m_staticText1.Wrap( -1 )
        self.m_staticText1.SetFont( wx.Font( 28, 70, 90, 90, False, wx.EmptyString ) )

        bSizer12.Add( self.m_staticText1, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5 )

        bSizer7.Add( bSizer12, 1, wx.EXPAND, 5 )

        bSizer4.Add( bSizer7, 0, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 5 )

        bSizer8 = wx.BoxSizer( wx.HORIZONTAL )

        bSizer13 = wx.BoxSizer( wx.VERTICAL )

        self.m_staticText3 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Time Module", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText3.Wrap( -1 )
        self.m_staticText3.SetFont( wx.Font( 20, 70, 90, 90, False, wx.EmptyString ) )

        bSizer13.Add( self.m_staticText3, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5 )

        bSizer8.Add( bSizer13, 1, wx.EXPAND, 5 )

        bSizer14 = wx.BoxSizer( wx.VERTICAL )

        self.m_staticText4 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"0", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText4.Wrap( -1 )
        self.m_staticText4.SetFont( wx.Font( 28, 70, 90, 90, False, wx.EmptyString ) )

        bSizer14.Add( self.m_staticText4, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 5 )

        bSizer8.Add( bSizer14, 1, wx.EXPAND, 5 )

        bSizer4.Add( bSizer8, 0, wx.EXPAND, 5 )

        bSizer2.Add( bSizer4, 1, wx.EXPAND, 5 )

        bSizer5 = wx.BoxSizer( wx.HORIZONTAL )

        self.m_start = wx.Button( self.m_panel1, wx.ID_ANY, u"Start", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer5.Add( self.m_start, 0, wx.ALL, 5 )

        self.m_stop = wx.Button( self.m_panel1, wx.ID_ANY, u"Stop", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer5.Add( self.m_stop, 0, wx.ALL, 5 )

        self.m_reset = wx.Button( self.m_panel1, wx.ID_ANY, u"Reset", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer5.Add( self.m_reset, 0, wx.ALL, 5 )

        bSizer2.Add( bSizer5, 0, wx.ALIGN_CENTER_HORIZONTAL, 5 )

        self.m_panel1.SetSizer( bSizer2 )
        self.m_panel1.Layout()
        bSizer2.Fit( self.m_panel1 )
        bSizer1.Add( self.m_panel1, 1, wx.EXPAND, 5 )

        self.SetSizer( bSizer1 )
        self.Layout()

        # Connect Events
        self.m_start.Bind( wx.EVT_BUTTON, self.m_startOnButtonClick )
        self.m_stop.Bind( wx.EVT_BUTTON, self.m_stopOnButtonClick )
        self.m_reset.Bind( wx.EVT_BUTTON, self.m_resetOnButtonClick )
        self.Bind(wx.EVT_TIMER, self.onUpdate, self.timer)

    def __del__( self ):
        pass

    # Virtual event handlers, overide them in your derived class
    def m_startOnButtonClick( self, event ):
        self.timer.Start(1)
        value = int(self.m_staticText1.GetLabelText())
        value_2 = int(self.m_staticText1.GetLabelText())
        self.staticText1_value = value
        self.staticText4_value = value_2
        global start_time
        start_time = time.time()

    def m_stopOnButtonClick( self, event ):
        self.timer.Stop()
        global start_time
        global elapsed_time
        elapsed_time = time.time() - start_time
        self.m_staticText4.SetLabel(str(int(elapsed_time*1000)))
        print(elapsed_time)

    def m_resetOnButtonClick( self, event ):
        self.m_staticText1.SetLabel("0")
        self.m_staticText4.SetLabel("0")

    def onUpdate( self, event ):
        global elapsed_time
        self.staticText1_value += 1
        self.m_staticText1.SetLabel(str(self.staticText1_value))

if __name__ == "__main__":
    app = wx.App(redirect=False)
    frame = MyFrame(None)
    app.SetTopWindow(frame)
    frame.Show(True)
    app.MainLoop()

Thanks in advance.
Kind regards
Ivo

0 个答案:

没有答案