【问题标题】:updating a value every n seconds每 n 秒更新一次值
【发布时间】:2015-04-16 00:14:55
【问题描述】:

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

我必须向测量仪器提出请求,并且我从该仪器获得 One 值。我想每 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_())

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

   def update(self):


        waarden=self.post_request(meter)
        self.data=[datetime.now(),values[6]]

在这里我尝试使用 Qtimer。我认为这会奏效,但它不会。我收到以下错误:

TypeError: connect() 槽参数应该是可调用的或信号,而不是 'Nonetype'

我不知道如何解决它。我对线程很感兴趣,但我认为这不利于我对 RPi 的 CPU 使用率。有谁知道我的问题的好方法吗?

提前致谢

【问题讨论】:

  • 替换:timer.timeout.connect(instrument.update())timer.timeout.connect(self.instrument.update)。换句话说,不要调用函数并传递结果。传递函数对象本身。
  • 对不起,它确实有效!!!! THANKKSSSSS
  • @DriesHeungens:函数是 Python 中的一等公民:您可以将它们作为参数传递给其他函数 f(g),从函数返回 return g,给一个新名称:h = g(两者hg 名称引用同一个函数对象),访问它们的属性 g.__name__ 等。要调用可调用对象,Python 使用括号 g()

标签: python multithreading timer pyqt4 self-updating


【解决方案1】:

这个:

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

应该是

timer.timeout.connect(instrument.update)

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2022-10-20
    相关资源
    最近更新 更多