你好python爱好者/专业人士,
我想使用FreeOpc library和PyQt&QtDesigner在python中创建OPC-UA客户端图形用户界面。我从一个简单的代码开始,其中每个DeltaT都会显示和更新两个过程值,其中DeltaT是用户指定的时间间隔。 (在我提供的代码中,未实现DeltaT。)
这是我使用的OPC服务器代码:
from opcua import Client
import time
url = "opc.tcp://localhost:4840"
client = Client(url)
client.connect()
print("Client Connected")
while True:
Temp = client.get_node("ns=2;i=2")
Temperature = Temp.get_value()
print(Temperature)
Press = client.get_node("ns=2;i=3")
Pressure = Press.get_value()
print(Pressure)
TIME = client.get_node("ns=2;i=4")
TIME_Value = TIME.get_value()
print(TIME_Value)
time.sleep(1)
这里是GUI的代码,该代码是通过首先使用QtDesigner设计布局,然后将文件格式从* .ui更改为* .py并进行一些修改(例如添加客户端规范)而创建的。
from PyQt5 import QtCore, QtGui, QtWidgets
from opcua import Client #added
import time #added
url = "opc.tcp://localhost:4840"
client = Client(url)
client.connect()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(80, 120, 195, 68))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setPointSize(14)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout_2.addWidget(self.label)
self.label_2 = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setPointSize(14)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.verticalLayout_2.addWidget(self.label_2)
self.horizontalLayout.addLayout(self.verticalLayout_2)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.widget)
font = QtGui.QFont()
font.setPointSize(14)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)
font = QtGui.QFont()
font.setPointSize(14)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setObjectName("lineEdit_2")
self.verticalLayout.addWidget(self.lineEdit_2)
self.horizontalLayout.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
Temp = client.get_node("ns=2;i=2")
Temperature = Temp.get_value()
self.lineEdit.setText(str(Temperature))
self.lineEdit.update()
Press = client.get_node("ns=2;i=3")
Pressure = Press.get_value()
self.lineEdit_2.setText(str(Pressure))
self.lineEdit_2.update()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Pressure"))
self.label_2.setText(_translate("MainWindow", "Temperature"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
结果是该窗口显示“ Pressure”和“ Temperature”的一个值,但在更改时不会更新该值。我应该添加哪些代码来确保会发生?实施用户定义的DeltaT(更新显示值的时间间隔)的好方法是什么?
我已经尝试研究opc-client-gui,但是由于我对Python和PyQt的了解不多,因此很难提取所需的信息。我的背景是Matlab,统计数据和能源系统建模。
版本:Python 3.6,PyQt5,Qt 5.9.1