pyqt开发-创建称类

上位机开发中,有时采集称的信号,从而达到称量的目的。这里用开关量来控制。

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)

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('出现问题')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值