pyqt5上位机开发框架
生活随笔
收集整理的這篇文章主要介紹了
pyqt5上位机开发框架
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from PyQt5.Qt import *
from resources.ui_main import Ui_MainWindow
from tool import TOOLclass Window(QMainWindow,Ui_MainWindow):def __init__(self):super().__init__()self.setupUi(self)self.pushButton_1.setCheckable(True)#初始化label狀態for i in range(4):self.change_label_color(f'light_{i}',status='init')def event_btn_clicked(self,checked):sender=self.sender()# 單例啟動TOOL.start(self,sender,self.call_back_light_color)def change_label_color(self,label_name,status='init'):''':param label_name: objectName:param status::return:'''color={'init':'#616161','green':'#00ff00','red':'#ff0000'}[status]style="""min-width:20px;min-heigth:20px;max-width:20px;max-heigth:20px;border-radius:10px;border:1px solid #f3f379;background-color:{};""".format(color)self.__getattribute__(label_name).setStyleSheet(style)def call_back_light_color(self,a,b):self.change_label_color('light_0','green')if a==1 else self.change_label_color('light_0','red')self.change_label_color('light_1','green')if b==1 else self.change_label_color('light_1','red')if __name__=="__main__":import sysapp=QApplication(sys.argv)win=Window()win.show()sys.exit(app.exec_())
from PyQt5.QtCore import QObject, QTimer, pyqtSignal
import threading
from siemens_modbus_tcp import Plcclass Tool(QObject):singal_lights=pyqtSignal(int,int)def __init__(self):super().__init__()self.parent=Noneself.sender= None#實例化Plc對象self.plc=Plc('192.168.5.100')def start(self,parent,sender,func):self.singal_lights.connect(func)t=threading.Thread(target=self.show_I)t.start()def show_I(self):import timewhile True:self.singal_lights.emit(self.plc.read_plc(0),self.plc.read_plc(1))time.sleep(1)TOOL=Tool()#實例化一個對象,單例模式
import time
import modbus_tk
import modbus_tk.defines as cts
import modbus_tk.modbus_tcp as modbus_tcpclass Plc(object):def __init__(self,host):try:self.master=modbus_tcp.TcpMaster(host=host)except Exception as e:print("連接失敗",e)def read_plc(self,address):try:data=self.master.execute(1,cts.READ_COILS,address,1)return data[0]except Exception as e:time.sleep(0.03)self.read_plc(self.master,address)def write_plc(self,address,value):try:self.master.execute(1,cts.WRITE_SINGLE_COIL,address,output_value=value)except Exception as e:time.sleep(0.003)self.write_plc(self.master,address,value)
總結
以上是生活随笔為你收集整理的pyqt5上位机开发框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大家在谈租房时都在谈些什么?--豆瓣租房
- 下一篇: 相同内容多个html如何合并单元格,合并