gpt4 book ai didi

python - 每 n 秒更新一个值

转载 作者:太空宇宙 更新时间:2023-11-04 03:37:08 24 4
gpt4 key购买 nike

我正在用 Python 创建一个实时应用程序(在 Raspberry Pi 上),我遇到了一个关于“每 5 秒更新一次程序中的值”的问题。我使用 Python 2.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_())

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

   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(instrument.update)

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

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

关于python - 每 n 秒更新一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28525272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com