上位机开发中,有时采集称的信号,从而达到称量的目的。这里用开关量来控制。
from PyQt5.QtCore import QObject, pyqtSignal
class Weight(QObject):
"""继承QObject 为了使用信号与槽"""
weight_signal = pyqtSignal(bool,bool,float)
def __init__(self,goal_weight,current_weight,before_weight):
super().__init__()
'''
:param goal_weight: 目标值
:param current_weight: 当前值
:param before_weight: 提前值
'''
self.goal_weight = goal_weight
self.current_weight = current_weight
self.before_weight = before_weight
#signal
self._accurate_door=None
self._rough_door=None
self.time_length=1000
self._id=None
def timerEvent(self, event) -> None:
super().timerEvent(event)
self.current_weight+=50
if self.current_weight>=self.goal_weight:
self.weight_signal.emit(False,False,self.current_weight)
self.killTimer(self._id) #停止定时器
return
elif self.before_weight<=self.current_weight<self.goal_weight:
self._accurate_door=True
self._rough_door=False
self.weight_signal.emit(self._accurate_door,self._rough_door,self.current_weight)
elif self.current_weight<self.before_weight:
self._accurate_door=True
self._rough_door=True
self.weight_signal.emit(self._accurate_door, self._rough_door, self.current_weight)
else:
raise StopIteration('称量出错')
def run(self):
self._id = self.startTimer(self.time_length)
def stop(self):
self.killTimer(self._id)
self.weight_signal.emit(False,False, self.current_weight)
![](https://img-blog.csdnimg.cn/img_convert/5a17bb1fab39be9fb1d35a3adbe965e5.png)
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setup_ui()
self.resize(600,550)
def setup_ui(self):
self.light=QLabel(self)
self.light.resize(50,50)
self.light.move(20,60)
self.light.setStyleSheet('border-radius:25px;background-color:gray')
self.light2 = QLabel(self)
self.light2.resize(50, 50)
self.light2.move(20, 140)
self.light2.setStyleSheet('border-radius:25px;background-color:gray')
self.le=QLineEdit(self)
self.le.resize(160,20)
self.le.move(300,40)
self.s=Weight(1000,0,700)
self.s.weight_signal.connect(self.signal_slot)
self.s.time_length=200
self.s.current_weight=100
self.s.goal_weight=2000
self.s.before_weight=1000
btn=QPushButton('CLICK',self)
btn.move(10,20)
btn.clicked.connect(lambda :self.s.run())
stop_btn=QPushButton('stop',self)
stop_btn.move(100,20)
stop_btn.clicked.connect(lambda :self.s.stop())
def signal_slot(self,a,b,c):
print(a,b,c)
if a:
self.light.setStyleSheet('border-radius:25px;background-color:green')
else:
self.light.setStyleSheet('border-radius:25px;background-color:red')
if b:
self.light2.setStyleSheet('border-radius:25px;background-color:green')
else:
self.light2.setStyleSheet('border-radius:25px;background-color:red')
self.le.setText(str(c))
if __name__=="__main__":
import sys
app=QApplication(sys.argv)
win=Window()
win.show()
sys.exit(app.exec_())
皮带电机类,注意,继承QObject后不要忘记Super()方法
from PyQt5.QtCore import QObject, pyqtSignal
class EleMac(QObject):
elec_signal=pyqtSignal(int,int,int)
def __init__(self):
super().__init__()
self.deviation_signal=None #跑偏信号
self.overload_signal = None #过载
self.overcurrent_signal= None #过流
self.elec_signal.connect(self.get_state)
def get_state(self,deviation_signal:bool,overload_signal:bool,overcurrent_signal:bool)->bool:
self.result=deviation_signal and overload_signal and overcurrent_signal #相当于串联
def start(self):
if self.result:
print('启动电机')
else:
print('出现问题')