如何在wxpython中正确设置计时器

问题描述 投票:0回答:1

我的问题是:如何正确使用Timer?有时我错过了数据。部分代码:

import wx
import serial 

store=[]
class all(wx.panel):
    def __init__(self, parent):
        …
        self.SetSizer( bSizer17 )
        self.Layout()

        self.timer1 = wx.Timer()
        self.timer1.SetOwner( self, 1 )

        self.timer2 = wx.Timer()
        self.timer2.SetOwner( self, 2 )
        self.timer2.Start( 500 )         ### running when app begins

        self.timer3 = wx.Timer()
        self.timer3.SetOwner( self, 3 )
        self.timer3.Start( 401)          ### running when app begins 

    def timer1(self,event):
        if self.timer1.IsRunning():
            self.timer1.Stop()
        else:
            self.timer1.Start( 100 ) 
    def timer2(self, event):
        event.Skip()
    def timer3(self, event):
        event.Skip()

    def timer1_plot(self, event)
         …
        plt.plot(x,y)
    def timer2_store( self, event ):   
        for line in ser:
            store.append(line)
        with open("C:\\Users\\Desktop\\saved_data.txt","a") as f: 
            for line in store:            
                f.writelines(str(line)+ "\n")
    def timer3_del( self, event ):           
        del store[:]

我每秒都有来自串行端口的数据。我使用数据来绘制和保存背景(与绘制无关)。为此,我使用了wxTimer。

我有3个按钮(与计时器绑定),其中2个被隐藏(2个计时器自动运行)。

1。按钮用于绘图。当我按下时,计时器运行。

2.button用于存储并写入.txt文件并从列表中删除

3.button用于删除列表中存储的数据

self.timer1.Start(100)#在按下按钮时运行

self.timer2.Start(500)#在应用程序启动时运行

self.timer3.Start(401)#在应用启动时运行

计时器之间的范围好吗?我应该自动运行其中两个吗?你有什么建议?任何帮助,将不胜感激。

python list timer save store
1个回答
0
投票

这已经很老了,所以您可能已经找到了解决方案...但这是我的建议。我的项目监视2个串行端口,一个具有一个每秒发送一次数据包的秤,另一个具有条形码扫描仪-数据到达时非常随机。

我每个串行端口使用1个线程,只是timer.sleep等待串行输入,并将其存储在共享对象中(每个线程1个对象)。然后,我有一个计时器来更新状态机,它查看每个共享对象,并相应地更新GUI。

如果这是我的项目,我建议进行以下更改:

  • 不要调用计时器事件.Skip(),在回调之后,不应链接其他任何东西。
  • 为每个串行端口或其他异步通信源创建一个线程。
  • 请确保将线程标记为守护进程,或者在关闭时将其显式杀死。
  • 使用一个可以更新所有内容的计时器,使用可以为用户带来满意响应的最低频率。

祝你好运。>>

© www.soinside.com 2019 - 2024. All rights reserved.