每n秒更新一次值

2024-10-02 04:29:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python创建一个实时应用程序(在Raspberry Pi上),我遇到了一个关于“每5秒更新一次程序中的值”的问题。我使用python2.7.9作为解释器,并用于GUI编程:PyQt4。在

我必须向一个测量仪器提出一个要求,然后从这个仪器中得到一个值。我想每5秒存储一次这个值。但我不想让程序等待,因为它必须做其他事情。无限while循环是不可能的。这是我的主程序代码:

class ApplicationWindow(QtGui.QMainWindow):
    def __init__(self):

        QtGui.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("application main window")
        self.setStyleSheet('background-color:#DBE0E4')
        self.file_menu = QtGui.QMenu('&File', self)
        self.file_menu.addAction('&Quit', self.fileQuit,
                              QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
        self.menuBar().addMenu(self.file_menu)

        self.help_menu = QtGui.QMenu('&Help', self)
        self.menuBar().addSeparator()
        self.menuBar().addMenu(self.help_menu)

        self.help_menu.addAction('&About', self.about)

        self.instrument=Online_meter('name','pasword')

        timer=QtCore.QTimer()
        timer.start(5000)
        timer.timeout.connect(instrument.update())



        self.main_widget = QtGui.QWidget(self)
        layout=QtGui.QGridLayout(self.main_widget)
        layout.setSpacing(10)
        layout.expandingDirections()

        time=Datetime() 
        dc = Plotgraph(self.main_widget)
        label1=Label(" Value:",False)
        label2=Label("waarde",True)

        layout.addWidget(dc,1,1,8,7)
        layout.addWidget(time,1,8,1,2)
        layout.addWidget(label1,2,8,1,2)
        layout.addWidget(label2,3,8,1,2)

        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)



   def fileQuit(self):
       self.close()

   def closeEvent(self, ce):
       self.fileQuit()

   def about(self):
       QtGui.QMessageBox.about(self, "About",
        """
       Copyright 2014 
       """

      )


qApp = QtGui.QApplication(sys.argv)

aw = ApplicationWindow()
aw.setWindowTitle("my app")
aw.showFullScreen()
sys.exit(qApp.exec_())

下面是我更新在线电表的方法代码:

^{pr2}$

在这里我试着用一个计时器。我以为这会起作用,但是不行。我得到了以下错误:

TypeError:connect()slot参数应为可调用或信号,而不是“Nonetype”

我不知道怎么解决它。我对线程化很感兴趣,但我认为这对我的RPi的CPU使用率没有好处。有人知道解决我问题的好办法吗?在

提前谢谢


Tags: selfmaindefhelpwidgetqtfilemenu
1条回答
网友
1楼 · 发布于 2024-10-02 04:29:49

这个:

timer.timeout.connect(instrument.update())

应该是的

^{pr2}$

前者在执行该行时立即调用函数;该函数返回None,这将导致.connect中的错误。后者只是将该函数连接到timeout插槽。在

第一次调用instrument.update应在5秒后发生

相关问题 更多 >

    热门问题