任务要求
- 用户输入S,开始租船;输入E,停止租船。
- 当一天结束后,统计出每条船的租借情况,打印出最短出租时间、最长出租时间等。
- 管理员定时向船只发送信号,若船只无法收到信号,则判断该船只已丢失。
外部支持库说明
- 使用PyQt5设计GUI,安装方法参考前文:PyQt5 安装教程
- 使用graphviz和pyreverse生成UML类图,graphviz安装参考前文:python graphviz画图
pyreverse安装:pip install pylint,类图生成命令:pyreverse -ASmy -o png a/ 注:a为存放代码的目录,使用前需将代码名称改为__init__.py,否则有以下报错
类的设计
总体类的设计较简单,答题分为两个类,一个是GUI部分,另一个是GUI的逻辑实现部分,其中rent_1类函数用于实现租船细节,showtime_1类函数用于计算时间,transform函数用于处理船只是否丢失。
结果分析
部分运行结果如图所示: 统计结果: 有关作者信息:
报错解决
- python执行函数,报错TypeError: rent_1() takes 1 positional argument but 2 were given
这个报错的意思是此函数只接收一个变量,而实际使用中传入了两个变量。这是因为我一开始在编写python类函数时,并没有在类函数参数列表中写入self,加入self即可。 所以,在python调用类对象时,必须要在类的函数的参数列表中写入self,以表示函数本身。 - 关于
QMessageBox.about() 函数 QMessageBox是QT里一种使用非常简单的小窗口,about函数用于填入内容,其函数原型为:
(method) about: (parent: QWidget, caption: str, text: str) -> None
所以,除此窗口本身外,该函数只能传入两个字符串型的参数。 3. 关于QTime 类的应用
self.timer.start(1000)
self.rent_time.stop()
开始计时,括号内单位为毫秒,表示多久更新一次
self.sum1 = temp.secsTo(rtime)
两个QTime类之间,可以使用secsTo() 函数计算时间差,精确到秒
系统分析总结
- 基于实用性考虑,调用数据库进行租赁信息的增删查改更为合理有效。
- 对于信号发射接收,可以考虑网络通讯和多线程。
全部源代码
import numbers
import threading
from tokenize import String
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit
from matplotlib import widgets
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt
import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(928, 826)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(280, 20, 341, 81))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(70, 230, 71, 31))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(70, 360, 71, 21))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(70, 490, 71, 21))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(70, 610, 71, 21))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(71, 135, 71, 31))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(200, 130, 151, 41))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(480, 130, 101, 41))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.label_8.setFont(font)
self.label_8.setObjectName("label_8")
self.label_9 = QtWidgets.QLabel(self.centralwidget)
self.label_9.setGeometry(QtCore.QRect(700, 140, 81, 21))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.label_9.setFont(font)
self.label_9.setObjectName("label_9")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(620, 720, 121, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(0, 170, 881, 16))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(230, 720, 121, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(13)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.textBrowser_9 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_9.setGeometry(QtCore.QRect(640, 80, 251, 31))
self.textBrowser_9.setObjectName("textBrowser_9")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(480, 210, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(690, 210, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
font.setBold(False)
font.setWeight(50)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(480, 340, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_6.setGeometry(QtCore.QRect(690, 340, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.pushButton_6.setFont(font)
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setGeometry(QtCore.QRect(480, 610, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.pushButton_7.setFont(font)
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_8.setGeometry(QtCore.QRect(480, 470, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.pushButton_8.setFont(font)
self.pushButton_8.setObjectName("pushButton_8")
self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_9.setGeometry(QtCore.QRect(690, 470, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.pushButton_9.setFont(font)
self.pushButton_9.setObjectName("pushButton_9")
self.pushButton_10 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_10.setGeometry(QtCore.QRect(690, 610, 91, 51))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(12)
self.pushButton_10.setFont(font)
self.pushButton_10.setObjectName("pushButton_10")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(210, 200, 171, 31))
self.textBrowser.setObjectName("textBrowser")
self.textBrowser_2 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_2.setGeometry(QtCore.QRect(210, 260, 171, 31))
self.textBrowser_2.setObjectName("textBrowser_2")
self.textBrowser_3 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_3.setGeometry(QtCore.QRect(210, 330, 171, 31))
self.textBrowser_3.setObjectName("textBrowser_3")
self.textBrowser_5 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_5.setGeometry(QtCore.QRect(210, 390, 171, 31))
self.textBrowser_5.setObjectName("textBrowser_5")
self.textBrowser_6 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_6.setGeometry(QtCore.QRect(210, 460, 171, 31))
self.textBrowser_6.setObjectName("textBrowser_6")
self.textBrowser_7 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_7.setGeometry(QtCore.QRect(210, 520, 171, 31))
self.textBrowser_7.setObjectName("textBrowser_7")
self.textBrowser_8 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_8.setGeometry(QtCore.QRect(210, 590, 171, 31))
self.textBrowser_8.setObjectName("textBrowser_8")
self.textBrowser_10 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_10.setGeometry(QtCore.QRect(210, 650, 171, 31))
self.textBrowser_10.setObjectName("textBrowser_10")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 928, 26))
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)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate(
"MainWindow", "LMY\'S Boat Renting System"))
self.label_2.setText(_translate("MainWindow", "boat 1"))
self.label_3.setText(_translate("MainWindow", "boat 2"))
self.label_4.setText(_translate("MainWindow", "boat 3"))
self.label_5.setText(_translate("MainWindow", "boat 4"))
self.label_6.setText(_translate("MainWindow", "ID"))
self.label_7.setText(_translate("MainWindow", "Renting Time"))
self.label_8.setText(_translate("MainWindow", "Renting"))
self.label_9.setText(_translate("MainWindow", "Status"))
self.pushButton.setText(_translate("MainWindow", "Sum"))
self.pushButton_2.setText(_translate("MainWindow", "Author"))
self.pushButton_3.setText(_translate("MainWindow", "S"))
self.pushButton_4.setText(_translate("MainWindow", "Safe"))
self.pushButton_5.setText(_translate("MainWindow", "S"))
self.pushButton_6.setText(_translate("MainWindow", "Safe"))
self.pushButton_7.setText(_translate("MainWindow", "S"))
self.pushButton_8.setText(_translate("MainWindow", "S"))
self.pushButton_9.setText(_translate("MainWindow", "Safe"))
self.pushButton_10.setText(_translate("MainWindow", "Safe"))
class NameController(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
global cnt
i = 0
self.flag_1 = "0"
self.sum1 = 0
self.sum2 = "0"
self.sum3 = "0"
self.sum4 = "0"
self.rent = []
self.overall_string = "Date : "
for i in range(4):
self.rent.append(0)
cnt = 1
push = 0
super().__init__()
self.setupUi(self)
time = QDateTime.currentDateTime()
timedisplay = time.toString("yyyy-MM-dd hh:mm:ss dddd")
rtime = QDateTime.currentDateTime()
rtimedisplay = rtime.toString("yyyy-MM-dd hh:mm:ss dddd")
self.textBrowser_9.setText(timedisplay)
self.timer = QTimer()
self.timer.start(1000)
self.rent_time = QTimer()
self.rent_time_2 = QTimer()
self.rent_time_3 = QTimer()
self.rent_time_4 = QTimer()
self.timer.timeout.connect(self.showtime)
self.pushButton_3.clicked.connect(self.rent_1)
self.pushButton_5.clicked.connect(self.rent_2)
self.pushButton_8.clicked.connect(self.rent_3)
self.pushButton_7.clicked.connect(self.rent_4)
self.pushButton_2.clicked.connect(lambda: QMessageBox.about(
self, "About the author", "Name: Liu MiYi\n Class: Computer Science\nDate: 2022.9.23"))
''' self.pushButton.clicked.connect(lambda: QMessageBox.about(
self, "About the renting of boats", self.overall_string)) '''
self.push_1 = 0
self.push_2 = 0
self.push_3 = 0
self.push_4 = 0
self.pushButton_4.clicked.connect(self.transform_1)
self.pushButton_6.clicked.connect(self.transform_2)
self.pushButton_9.clicked.connect(self.transform_3)
self.pushButton_10.clicked.connect(self.transform_4)
def transform_1(self):
self.pushButton_4.setText("Safe")
def transform_2(self):
self.pushButton_6.setText("Safe")
def transform_3(self):
self.pushButton_9.setText("Safe")
def transform_4(self):
self.pushButton_10.setText("Safe")
def rent_1(self):
self.push_1 = self.push_1+1
self.rent_time.start(1000)
self.rent_time.timeout.connect(self.showtime_1)
self.pushButton_3.setText("E")
if(self.push_1 == 1):
rttime = QDateTime.currentDateTime()
global temp
temp = rttime
rttimedisplay = rttime.toString("yyyy-MM-dd hh:mm:ss dddd")
rt = rttimedisplay[0:19]
okl = int(rttimedisplay[11:13])
if(okl < 13):
self.overall_string = self.overall_string + \
rttimedisplay[5:10]+" Morning\nBoat1 Renting Time : "
else:
self.overall_string = self.overall_string + \
rttimedisplay[5:10]+" Afternoon\nBoat1 Renting Time : "
self.textBrowser.setText(rt)
if(self.push_1 == 2):
self.pushButton_3.setText("S")
self.rent_time.stop()
self.overall_string = self.overall_string + \
str(self.rent[0])+"seconds\nBoat1 Renting Time: "
def showtime_1(self):
rtime = QDateTime.currentDateTime()
rtimedisplay = rtime.toString("yyyy-MM-dd hh:mm:ss dddd")
r = rtimedisplay[0:19]
self.flag_1 = rtimedisplay[18]
if(self.flag_1 == "5"):
self.pushButton_4.setText("Lost")
self.textBrowser_2.setText(r)
self.sum1 = temp.secsTo(rtime)
self.rent[0] = self.sum1
def rent_2(self):
self.push_2 = self.push_2+1
self.rent_time_2.start(1000)
self.rent_time_2.timeout.connect(self.showtime_2)
self.pushButton_5.setText("E")
if(self.push_2 == 1):
rttime_2 = QDateTime.currentDateTime()
rttimedisplay_2 = rttime_2.toString("yyyy-MM-dd hh:mm:ss dddd")
rt_2 = rttimedisplay_2[0:19]
global temp2
temp2 = rttime_2
self.textBrowser_3.setText(rt_2)
if(self.push_2 == 2):
self.pushButton_5.setText("S")
self.rent_time_2.stop()
self.overall_string = self.overall_string + \
str(self.rent[1])+" seconds\n"+"Boat3 Renting Time : "
def showtime_2(self):
rtime = QDateTime.currentDateTime()
rtimedisplay = rtime.toString("yyyy-MM-dd hh:mm:ss dddd")
r = rtimedisplay[0:19]
self.flag_2 = rtimedisplay[18]
if(self.flag_2 == "5"):
self.pushButton_6.setText("Lost")
self.textBrowser_5.setText(r)
self.sum2 = temp2.secsTo(rtime)
self.rent[1] = self.sum2
def rent_3(self):
self.push_3 = self.push_3+1
self.rent_time_3.start(1000)
self.rent_time_3.timeout.connect(self.showtime_3)
self.pushButton_8.setText("E")
if(self.push_3 == 1):
rttime_3 = QDateTime.currentDateTime()
rttimedisplay_3 = rttime_3.toString("yyyy-MM-dd hh:mm:ss dddd")
global temp3
temp3 = rttime_3
rt_3 = rttimedisplay_3[0:19]
self.textBrowser_6.setText(rt_3)
if(self.push_3 == 2):
self.pushButton_8.setText("S")
self.rent_time_3.stop()
self.overall_string = self.overall_string + \
str(self.rent[2])+" seconds\n"+"Boat4 Renting Time : "
def showtime_3(self):
rtime = QDateTime.currentDateTime()
rtimedisplay = rtime.toString("yyyy-MM-dd hh:mm:ss dddd")
r = rtimedisplay[0:19]
self.flag_3 = rtimedisplay[18]
self.sum3 = temp3.secsTo(rtime)
self.rent[2] = self.sum3
if(self.flag_3 == "5"):
self.pushButton_9.setText("Lost")
self.textBrowser_7.setText(r)
def rent_4(self):
self.push_4 = self.push_4+1
self.rent_time_4.start(1000)
self.rent_time_4.timeout.connect(self.showtime_4)
self.pushButton_7.setText("E")
if(self.push_4 == 1):
rttime_4 = QDateTime.currentDateTime()
rttimedisplay_4 = rttime_4.toString("yyyy-MM-dd hh:mm:ss dddd")
global temp4
temp4 = rttime_4
rt_4 = rttimedisplay_4[0:19]
self.textBrowser_8.setText(rt_4)
if(self.push_4 == 2):
self.pushButton_7.setText("S")
self.rent_time_4.stop()
num_boats = 0
if(self.pushButton_4.text() == "Safe"):
num_boats = num_boats+1
if(self.pushButton_6.text() == "Safe"):
num_boats = num_boats+1
if(self.pushButton_9.text() == "Safe"):
num_boats = num_boats+1
if(self.pushButton_10.text() == "Safe"):
num_boats = num_boats+1
self.overall_string = self.overall_string + \
str(self.rent[3])+" seconds\nMax Renting Time : " + \
str(max(self.rent))+" seconds\nMin Renting Time : " + \
str(min(self.rent)) + \
" seconds\n"+"The Number of Boats :" + str(num_boats)
self.pushButton.clicked.connect(lambda: QMessageBox.about(
self, "About the renting of boats", self.overall_string))
def showtime_4(self):
rtime = QDateTime.currentDateTime()
rtimedisplay = rtime.toString("yyyy-MM-dd hh:mm:ss dddd")
r = rtimedisplay[0:19]
self.flag_4 = rtimedisplay[18]
self.sum4 = temp4.secsTo(rtime)
self.rent[3] = self.sum4
if(self.flag_4 == "5"):
self.pushButton_10.setText("Lost")
self.textBrowser_10.setText(r)
def showtime(self):
if(cnt):
time = QDateTime.currentDateTime()
timedisplay = time.toString("yyyy-MM-dd hh:mm:ss dddd")
self.textBrowser_9.setText(timedisplay)
else:
self.timer.stop()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = NameController()
window.show()
sys.exit(app.exec_())
|