QT之电梯实现

PyQt5模拟电梯运行

电梯运行策略:

    1.电梯上行:只处理上楼事件,若有下楼事件,则记录在downFloorList中,待上楼事件处理完后,再处理下楼事件

    2.电梯下行: 与上行反之

# coding=utf-8
import sys

import sys, os
if hasattr(sys, 'frozen'):
    os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class Elevator(QWidget):
    aimFloor=1
    floor = 1 #电梯当前楼层,初试电梯在1楼
    prefloor = 1#表示电梯上下初始运行所处的楼层
    status = 0 #电梯当前状态:0表示暂停,1表示向上,-1表示向下
    preStatus = 1 #电梯前一状态,1表示向上,-1表示向下
    door = 1 #0表示打开,1表示关闭
    upfloorList = [] #电梯上行楼层
    downfloorList = [] #电梯下行楼层
    buttonList = []#电梯楼层按钮
    floorList=[]

    def __init__(self,buttonCount):
        super().__init__()
        self.initUI(buttonCount)
        self.timer2 = QTimer(self)  # 初始化一个定时器
        self.timer2.timeout.connect(lambda: self.changeFloor())  # 计时结束调用楼层显示方法
        self.timer2.start(1500)

    def initUI(self,buttonCount):
        lay = QGridLayout()
        self.lcd2 = QLCDNumber()#说明电梯当前处于楼层
        self.label = QLabel("状态")#说明电梯状态即方向
        self.label.setStyleSheet("QLabel{color:blue}")
        self.lcd2.setSegmentStyle(QLCDNumber.Flat)
        self.lcd2.setStyleSheet("border:0.5px solid black; color: red; background: silver;")
        self.lcd2.display(1)
        lay.addWidget(self.lcd2,0,0,1,1)
        lay.addWidget(self.label,0,2,1,2)
        for i in range(buttonCount):
            button = QPushButton(str(i+1))
            button.clicked.connect(self.Cli)
            self.buttonList.append(button)
            lay.addWidget(button)
        self.close_door = QPushButton("关门")
        self.open_door = QPushButton("开门")
        self.close_door.clicked.connect(self.Close)
        self.open_door.clicked.connect(self.Open)
        lay.addWidget(self.close_door)
        lay.addWidget(self.open_door)
        lay.addWidget(button)
        self.setLayout(lay)
        self.show()
    #"QPushButton{color:black}", "QPushButton{background-color:rgb(78,255,255)}"

    #电梯内部按钮
    def Cli(self):
        floor = int(self.sender().text())
        if self.floor==floor:
            self.Open()
            return
        elif floor > self.floor and floor not in self.upfloorList :
             self.upfloorList.append(floor)
             if self.status == 0 and self.preStatus==1:
                 self.preStatus=1
                 self.status=1
        elif floor < self.floor and floor not in self.downfloorList:
            self.downfloorList.append(floor)
            if self.status == 0 and self.preStatus==-1:
                self.preStatus = -1
                self.status = -1
        self.sender().setStyleSheet( "QPushButton{background-color:red}")
        self.Close()

    #当电梯门打开后才执行闭门函数
    def Close(self):
        if self.door == 0: #=0表示当层楼门打开
            self.door = 1 #改变门状态为关闭,门关闭才代表电梯可以走
            self.status = self.preStatus
            self.label.setText("电梯门关闭")
            self.close_door.setStyleSheet("QPushButton{background-color:red}")
            self.open_door.setStyleSheet("QPushButton{background-color:None}")
        self.runStrategy()
        self.timer2.start(1500)#关门开始循环计时


    def Open(self):
        if self.door==1:
            self.door = 0
            self.preStatus = self.status
            self.status = 0
            self.label.setText("电梯门打开")
            self.open_door.setStyleSheet("QPushButton{background-color:red}")
            self.close_door.setStyleSheet("QPushButton{background-color:None}")
            self.timer2.stop() #开门停止循环计时
        self.buttonList[self.floor - 1].setStyleSheet("QPushButton{background-color:None}")


    #电梯运行策略函数
    def runStrategy(self):
        if self.status == 1: #电梯上行状态
            if len(self.upfloorList)==0 and len(self.downfloorList)!=0: #上行楼层为空,下行楼层不为空
                self.status=-1                                          #改变电梯状态为下行
                self.label.setText("电梯下行")
            elif len(self.upfloorList)!=0:                              #如果上行楼层不为空,则电梯继续上行
                self.label.setText("电梯上行")
        elif self.status == -1:   #电梯下行状态
            if len(self.downfloorList) == 0 and len(self.upfloorList) != 0:  #下行楼层为空,上行楼层不为空
                self.status = 1
                self.label.setText("电梯上行")
            elif len(self.downfloorList)!=0:
                self.label.setText("电梯下行")
        if len(self.downfloorList)==0 and len(self.upfloorList)==0:
                self.status=0
                self.label.setText("电梯停滞")
                self.timer2.stop() #防止无效循环运行,耗费内存


    #楼层显示改变
    def changeFloor(self):
        self.runStrategy() #先来判断电梯运行状态
        print("还在运行")
        if self.status == 1 and len(self.upfloorList)!=0 and self.aimFloor!= max(self.upfloorList) :
            self.aimFloor=max(self.upfloorList)
            self.floorList = []
            for i in range(self.floor+1,self.aimFloor+1):
                self.floorList.append(i)
        elif self.status == -1 and len(self.downfloorList)!=0  and self.aimFloor!= min(self.downfloorList):
            self.aimFloor = min(self.downfloorList)
            self.floorList = []
            for i in range(self.aimFloor , self.floor ):
                self.floorList.append(i)
            self.floorList.sort(reverse=True)
        if self.status!=0:
            self.displayFloor()


    #楼层显示
    def displayFloor(self):
        if len(self.floorList)!=0 and self.door==1:
            self.floor=self.floorList.pop(0)
            self.lcd2.display(self.floor)
            if self.status==1 and self.floor in self.upfloorList:
                self.upfloorList.remove(self.floor)
                self.Open()  # 当层楼到了,电梯开门
            elif self.status==-1 and self.floor in self.downfloorList:
                self.downfloorList.remove(self.floor)
                self.Open()  # 当层楼到了,电梯开门




if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Elevator(10)
    app.exit(app.exec_())

 效果:

这个效果是时间bug修复之前版本,上述代码是bug修复的,应该不存在bug了,如果存在,欢迎大家指出。

ScreenCapture

PYQT5配置教程:https://blog.csdn.net/hi_sir_destroy/article/details/103921510 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值