wxPython StopWatch 不断显示计数时间?

发布于 2024-12-17 01:47:06 字数 1440 浏览 0 评论 0原文

我正在尝试使用 wxPython 创建一个 GUI 计时器。单击“开始”按钮,计时器启动,并且标签显示计时器从秒数到分钟数,甚至可能到小时数。停止按钮停止计时器。我不知道如何让计时器不断显示在标签中。我尝试了 while True 循环,但似乎 SetLabel() 或 Time() 想要显示一次并正在等待循环结束。

import wx

class Timer(wx.Frame):

    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(400, 350))

        self.main()
        self.Centre()
        self.Show()

    def main(self):

        panel = wx.Panel(self)
        sizer = wx.GridBagSizer(5, 0)

        self.timer = wx.StopWatch()

        self.label = wx.StaticText(panel)
        sizer.Add(self.label, pos=(0, 0), flag=wx.ALIGN_CENTER)

        button_start = wx.Button(panel, label='Start')
        self.Bind(wx.EVT_BUTTON, self.OnStart, button_start)
        sizer.Add(button_start, pos=(1, 0), flag=wx.ALIGN_CENTER)

        button_stop = wx.Button(panel, label='Stop')
        self.Bind(wx.EVT_BUTTON, self.OnStop, button_stop)
        sizer.Add(button_stop, pos=(2, 0), flag=wx.ALIGN_CENTER)

        sizer.AddGrowableRow(0)
        sizer.AddGrowableRow(1)
        sizer.AddGrowableRow(2)
        sizer.AddGrowableCol(0)

        panel.SetSizer(sizer)

    def OnStart(self, event):

        self.timer.Start()
        while True:
            self.label.SetLabel(str(self.timer.Time()))

    def OnStop(self, event):

        self.timer.Pause()

if __name__ == '__main__':

    app = wx.App()
    Timer(None, 'Timer')
    app.MainLoop()

I'm trying to create a GUI timer using wxPython. A Start button is clicked, the timer starts and a label displays the timer counting the seconds up to minutes and maybe to hours. A Stop button stops the timer. I don't know how to have the timer constantly displayed in the label. I tried a while True loop but it seems SetLabel() or Time() wants to display once and is waiting for an end to the loop.

import wx

class Timer(wx.Frame):

    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(400, 350))

        self.main()
        self.Centre()
        self.Show()

    def main(self):

        panel = wx.Panel(self)
        sizer = wx.GridBagSizer(5, 0)

        self.timer = wx.StopWatch()

        self.label = wx.StaticText(panel)
        sizer.Add(self.label, pos=(0, 0), flag=wx.ALIGN_CENTER)

        button_start = wx.Button(panel, label='Start')
        self.Bind(wx.EVT_BUTTON, self.OnStart, button_start)
        sizer.Add(button_start, pos=(1, 0), flag=wx.ALIGN_CENTER)

        button_stop = wx.Button(panel, label='Stop')
        self.Bind(wx.EVT_BUTTON, self.OnStop, button_stop)
        sizer.Add(button_stop, pos=(2, 0), flag=wx.ALIGN_CENTER)

        sizer.AddGrowableRow(0)
        sizer.AddGrowableRow(1)
        sizer.AddGrowableRow(2)
        sizer.AddGrowableCol(0)

        panel.SetSizer(sizer)

    def OnStart(self, event):

        self.timer.Start()
        while True:
            self.label.SetLabel(str(self.timer.Time()))

    def OnStop(self, event):

        self.timer.Pause()

if __name__ == '__main__':

    app = wx.App()
    Timer(None, 'Timer')
    app.MainLoop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁憨憨 2024-12-24 01:47:06

您永远不会从 OnStart 返回,因此事件循环挂起,等待事件处理程序完成。所有事件处理程序都需要立即返回,以便可以处理其他事件。使用 wx.Timer 处理程序来更新秒表标签。

You never return from OnStart, so the event loop is hung waiting for the event handler to finish. All event handlers need to return immediately so that other events can be processed. Use a wx.Timer handler to update the stopwatch label.

浊酒尽余欢 2024-12-24 01:47:06

听起来您想从 00:00:00 开始计数,对吧?您应该看一下 wxPython 演示中的 LEDNumberCtrl 演示。其中的第三个示例与您正在寻找的非常相似。我认为你可以用最少的摆弄让它工作。

It sounds like you want to start from 00:00:00 and count up, right? You should take a look at the LEDNumberCtrl demo in the wxPython demo. The third example in it is very similar to what you're looking for. I think you could get it to work with minimal fiddling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文