我正在尝试将其设置为用户将数字输入到文本框中,然后运行计算以确定每个不同时段中的连续时间,加班时间和加倍时间。
我分别编写了计算结果,并通过IDLE验证了其工作原理。我还运行了GUI并验证了它的工作原理,但是当我将两者合并在一起时,每当我按下按钮进行计算时它就会崩溃。我正在用退出代码-1073740791(0xC0000409)完成处理。我想这是试图在一个函数中存储许多if语句的问题,但是我不确定。任何指针或旅行将不胜感激。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Python\Hours.1\GUI.Hours.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(594, 678)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(130, 140, 122, 429))
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.lineEdit_2 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.verticalLayout.addWidget(self.lineEdit_2)
self.lineEdit_3 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.verticalLayout.addWidget(self.lineEdit_3)
self.lineEdit_4 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_4.setObjectName("lineEdit_4")
self.verticalLayout.addWidget(self.lineEdit_4)
self.lineEdit_5 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_5.setObjectName("lineEdit_5")
self.verticalLayout.addWidget(self.lineEdit_5)
self.lineEdit_6 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_6.setObjectName("lineEdit_6")
self.verticalLayout.addWidget(self.lineEdit_6)
self.lineEdit_7 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_7.setObjectName("lineEdit_7")
self.verticalLayout.addWidget(self.lineEdit_7)
self.lineEdit_8 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_8.setObjectName("lineEdit_8")
self.verticalLayout.addWidget(self.lineEdit_8)
self.lineEdit_9 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_9.setObjectName("lineEdit_9")
self.verticalLayout.addWidget(self.lineEdit_9)
self.lineEdit_10 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_10.setObjectName("lineEdit_10")
self.verticalLayout.addWidget(self.lineEdit_10)
self.lineEdit_11 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_11.setObjectName("lineEdit_11")
self.verticalLayout.addWidget(self.lineEdit_11)
self.lineEdit_12 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_12.setObjectName("lineEdit_12")
self.verticalLayout.addWidget(self.lineEdit_12)
self.lineEdit_13 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_13.setObjectName("lineEdit_13")
self.verticalLayout.addWidget(self.lineEdit_13)
self.lineEdit_14 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_14.setObjectName("lineEdit_14")
self.verticalLayout.addWidget(self.lineEdit_14)
self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget1.setGeometry(QtCore.QRect(8, 140, 111, 431))
self.layoutWidget1.setObjectName("layoutWidget1")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget1)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.Monday = QtWidgets.QLabel(self.layoutWidget1)
self.Monday.setObjectName("Monday")
self.verticalLayout_2.addWidget(self.Monday)
self.Tuesday = QtWidgets.QLabel(self.layoutWidget1)
self.Tuesday.setObjectName("Tuesday")
self.verticalLayout_2.addWidget(self.Tuesday)
self.Wednesday = QtWidgets.QLabel(self.layoutWidget1)
self.Wednesday.setObjectName("Wednesday")
self.verticalLayout_2.addWidget(self.Wednesday)
self.Thursday = QtWidgets.QLabel(self.layoutWidget1)
self.Thursday.setObjectName("Thursday")
self.verticalLayout_2.addWidget(self.Thursday)
self.Friday = QtWidgets.QLabel(self.layoutWidget1)
self.Friday.setObjectName("Friday")
self.verticalLayout_2.addWidget(self.Friday)
self.Saturday = QtWidgets.QLabel(self.layoutWidget1)
self.Saturday.setObjectName("Saturday")
self.verticalLayout_2.addWidget(self.Saturday)
self.Sunday = QtWidgets.QLabel(self.layoutWidget1)
self.Sunday.setObjectName("Sunday")
self.verticalLayout_2.addWidget(self.Sunday)
self.Monday2 = QtWidgets.QLabel(self.layoutWidget1)
self.Monday2.setObjectName("Monday2")
self.verticalLayout_2.addWidget(self.Monday2)
self.Tuesday2 = QtWidgets.QLabel(self.layoutWidget1)
self.Tuesday2.setObjectName("Tuesday2")
self.verticalLayout_2.addWidget(self.Tuesday2)
self.Wednesday2 = QtWidgets.QLabel(self.layoutWidget1)
self.Wednesday2.setObjectName("Wednesday2")
self.verticalLayout_2.addWidget(self.Wednesday2)
self.Thursday2 = QtWidgets.QLabel(self.layoutWidget1)
self.Thursday2.setObjectName("Thursday2")
self.verticalLayout_2.addWidget(self.Thursday2)
self.Friday2 = QtWidgets.QLabel(self.layoutWidget1)
self.Friday2.setObjectName("Friday2")
self.verticalLayout_2.addWidget(self.Friday2)
self.Saturday2 = QtWidgets.QLabel(self.layoutWidget1)
self.Saturday2.setObjectName("Saturday2")
self.verticalLayout_2.addWidget(self.Saturday2)
self.Sunday2 = QtWidgets.QLabel(self.layoutWidget1)
self.Sunday2.setObjectName("Sunday2")
self.verticalLayout_2.addWidget(self.Sunday2)
self.layoutWidget2 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget2.setGeometry(QtCore.QRect(460, 260, 81, 151))
self.layoutWidget2.setObjectName("layoutWidget2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.layoutWidget2)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.lcdNumber = QtWidgets.QLCDNumber(self.layoutWidget2)
self.lcdNumber.setObjectName("lcdNumber")
self.verticalLayout_3.addWidget(self.lcdNumber)
self.lcdNumber_2 = QtWidgets.QLCDNumber(self.layoutWidget2)
self.lcdNumber_2.setObjectName("lcdNumber_2")
self.verticalLayout_3.addWidget(self.lcdNumber_2)
self.lcdNumber_3 = QtWidgets.QLCDNumber(self.layoutWidget2)
self.lcdNumber_3.setObjectName("lcdNumber_3")
self.verticalLayout_3.addWidget(self.lcdNumber_3)
self.layoutWidget3 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget3.setGeometry(QtCore.QRect(358, 260, 91, 151))
self.layoutWidget3.setObjectName("layoutWidget3")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.layoutWidget3)
self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.Straight_Time = QtWidgets.QLabel(self.layoutWidget3)
self.Straight_Time.setObjectName("Straight_Time")
self.verticalLayout_4.addWidget(self.Straight_Time)
self.Overtime = QtWidgets.QLabel(self.layoutWidget3)
self.Overtime.setObjectName("Overtime")
self.verticalLayout_4.addWidget(self.Overtime)
self.DoubleOvertime = QtWidgets.QLabel(self.layoutWidget3)
self.DoubleOvertime.setObjectName("DoubleOvertime")
self.verticalLayout_4.addWidget(self.DoubleOvertime)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(350, 430, 191, 51))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 594, 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)
self.pushButton.clicked.connect(self.CalHour)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Monday.setText(_translate("MainWindow", "Monday"))
self.Tuesday.setText(_translate("MainWindow", "Tuesday"))
self.Wednesday.setText(_translate("MainWindow", "Wednesday"))
self.Thursday.setText(_translate("MainWindow", "Thursday"))
self.Friday.setText(_translate("MainWindow", "Friday"))
self.Saturday.setText(_translate("MainWindow", "Saturday"))
self.Sunday.setText(_translate("MainWindow", "Sunday"))
self.Monday2.setText(_translate("MainWindow", "Monday 2"))
self.Tuesday2.setText(_translate("MainWindow", "Tuesday 2"))
self.Wednesday2.setText(_translate("MainWindow", "Wednesday 2"))
self.Thursday2.setText(_translate("MainWindow", "Thursday 2"))
self.Friday2.setText(_translate("MainWindow", "Friday 2"))
self.Saturday2.setText(_translate("MainWindow", "Saturday 2"))
self.Sunday2.setText(_translate("MainWindow", "Sunday 2"))
self.Straight_Time.setText(_translate("MainWindow", "Straight Time"))
self.Overtime.setText(_translate("MainWindow", "Overtime"))
self.DoubleOvertime.setText(_translate("MainWindow", "Double Overtime"))
self.pushButton.setText(_translate("MainWindow", "Calculate"))
def CalHour(self):
Monday_1 = self.lineEdit.text()
Tuesday_1 = self.lineEdit_2.text()
Wednesday_1 = self.lineEdit_3.text()
Thursday_1 = self.lineEdit_4.text()
Friday_1 = self.lineEdit_5.text()
Saturday_1 = self.lineEdit_6()
Sunday_1 = self.lineEdit_7.text()
#Convert Text to Number
Mon1 = float(Monday_1)
Tue1 = float(Tuesday_1)
Wed1 = float(Wednesday_1)
Thur1 = float(Thursday_1)
Fri1 = float(Friday_1)
Sat1 = float(Saturday_1)
Sun1 = float(Sunday_1)
# Daily Straight Time Calculation
if 0 <= Mon1 <= 8:
Mon_straight_time = Mon1
else:
Mon_straight_time = 8
if 0 <= Tue1 <= 8:
Tue_straight_time = Tue1
else:
Tue_straight_time = 8
if 0 <= Wed1 <= 8:
Wed_straight_time = Wed1
else:
Wed_straight_time = 8
if 0 <= Thur1 <= 8:
Thur_straight_time = Thur1
else:
Thur_straight_time = 8
if 0 <= Fri1 <= 8:
Fri_straight_time = Fri1
else:
Fri_straight_time = 8
if 0 <= Sat1 <= 8:
Sat_straight_time = Sat1
else:
Sat_straight_time = 8
if 0 <= Sun1 <= 8:
Sun_straight_time = Sun1
else:
Sun_straight_time = 8
# Weekly Straight time calculation
Weekly_straight_time = (Mon_straight_time + Tue_straight_time +
Wed_straight_time + Thur_straight_time +
Fri_straight_time + Sat_straight_time + Sun_straight_time)
# Evaluate Daily Overtime
if 8 < Mon1 <= 12:
Mon_overtime = Mon1 - 8
elif 8 < Mon1 > 12:
Mon_overtime = 4
else:
Mon_overtime = 0
if 8 < Tue1 <= 12:
Tue_overtime = Tue1 - 8
elif 8 < Tue1 > 12:
Tue_overtime = 4
else:
Tue_overtime = 0
if 8 < Wed1 <= 12:
Wed_overtime = Wed1 - 8
elif 8 < Wed1 > 12:
Wed_overtime = 4
else:
Wed_overtime = 0
if 8 < Thur1 <= 12:
Thur_overtime = Thur1 - 8
elif 8 < Thur1 > 12:
Thur_overtime = 4
else:
Thur_overtime = 0
if 8 < Fri1 <= 12:
Fri_overtime = Fri1 - 8
elif 8 < Fri1 > 12:
Fri_overtime = 4
else:
Fri_overtime = 0
# Saturday Overtime and Double-time
if Weekly_straight_time >= 40 and 12 >= Sat1 > 0:
Sat_overtime = Sat1
Sat_double_time = 0
elif Weekly_straight_time >= 40 and 12 <= Sat1:
Sat_overtime = 12
Sat_double_time = Sat1 - 12
# elif Weekly_straight_time < 40 and Sat1 > 0 and Sat1 + Weekly_straight_time <= 40:
# Sat1 + Weekly_straight_time
elif Weekly_straight_time < 40 and Sat1 > 0 and Sat1 + Weekly_straight_time > 40 and Sat1 <= 12:
Sat_overtime = ((Sat1 + Weekly_straight_time) - 40)
# Weekly_straight_time = 40
elif Weekly_straight_time < 40 and Sat1 > 0 and Sat1 + Weekly_straight_time > 40 and Sat1 > 12:
Sat_overtime = ((Sat1 + Weekly_straight_time) - 40)
# Weekly_straight_time = 40
Sat_double_time = (Sat1 - 12)
elif Weekly_straight_time < 40 and Sat1 > 0 and Sat1 + Weekly_straight_time <= 40 and Sat1 > 12:
Sat_overtime = 4
# Weekly_straight_time = 40
Sat_double_time = (Sat1 - 12)
elif Sat1 == 0:
Sat_overtime = 0
Sat_double_time = 0
elif Sat1 < 12:
Sat_double_time = 0
else:
Sat_overtime = 0
# Add Total Overtime to this point Mon-Sat
Mon_Sat_Overtime = Mon_overtime + Tue_overtime + Wed_overtime + Thur_overtime + Fri_overtime + Sat_overtime
# Sunday Overtime
if Weekly_straight_time >= 40 and 12 >= Sun1 > 0 and (
Mon_overtime + Tue_overtime + Wed_overtime + Thur_overtime + Fri_overtime + Sat_overtime) < 40 and (
Sun1 + (
Mon_overtime + Tue_overtime + Wed_overtime + Thur_overtime + Fri_overtime + Sat_overtime)) <= 40:
Sun_overtime = Sun1
Sun_double_time = 0
elif Weekly_straight_time >= 40 and 12 <= Sun1 > 0 and (
Mon_overtime + Tue_overtime + Wed_overtime + Thur_overtime + Fri_overtime + Sat_overtime) < 40 and (
Sun1 + (Mon_overtime + Tue_overtime + Wed_overtime + Thur_overtime + Fri_overtime + Sat_overtime)) > 40:
Sun_double_time = ((Sun1 + Mon_Sat_Overtime) - 40)
Overtime = 40
Sun_overtime = Sun1
elif Weekly_straight_time >= 40 and 12 <= Sun1 > 0 and (
Mon_overtime + Tue_overtime + Wed_overtime + Thur_overtime + Fri_overtime + Sat_overtime) < 40 and (
Sun1 + (Mon_overtime + Tue_overtime + Wed_overtime + Thur_overtime + Fri_overtime + Sat_overtime)) < 40:
Sun_double_time = Sun1 - 12
Sun_overtime = 12
# elif Weekly_straight_time == 40 and 12 >= Sun1:
# Sun_overtime = Sun1
# elif Weekly_straight_time == 40 and 12 < Sun1:
# Sun_overtime = 12
# Sun_double_time = (Sun1 - 12)
# elif Weekly_straight_time < 40 and Sun1 >0 and Sun1 + Weekly_straight_time <= 40:
# Sun1 + Weekly_straight_time
elif Weekly_straight_time < 40 and Sun1 > 0 and Sun1 + Weekly_straight_time > 40 and Sun1 <= 12:
Sun_overtime = ((Sun1 + Weekly_straight_time) - 40)
# Weekly_straight_time = 40
elif Weekly_straight_time < 40 and Sun1 > 0 and Sun1 + Weekly_straight_time > 40 and Sun1 > 12:
Sun1 = ((12 + Weekly_straight_time) - 40)
# Weekly_straight_time = 40
Sun_double_time = (Sat1 - 12)
elif Weekly_straight_time < 40 and Sun1 > 0 and Sun1 + Weekly_straight_time <= 40 and Sun1 > 12:
# Sun1 = ((12 + Weekly_straight_time) - 40)
# Weekly_straight_time = 40
Sun_double_time = (Sun1 - 12)
Sun_overtime = 4
elif Sun1 == 0:
Sun_double_time = 0
Sun_overtime = 0
# Double Time Calculations
if Mon1 > 12:
Mon_double_time = Mon1 - 12
elif Mon1 < 12:
Mon_double_time = 0
if Tue1 > 12:
Tue_double_time = Tue1 - 12
elif Tue1 < 12:
Tue_double_time = 0
if Wed1 > 12:
Wed_double_time = Wed1 - 12
elif Wed1 < 12:
Wed_double_time = 0
if Thur1 > 12:
Thur_double_time = Thur1 - 12
elif Thur1 < 12:
Thur_double_time = 0
if Fri1 > 12:
Fri_double_time = Fri1 - 12
elif Fri1 < 12:
Fri_double_time = 0
Weekly_double_time = (
Mon_double_time + Tue_double_time + Wed_double_time + Thur_double_time + Fri_double_time + Sat_double_time + Sun_double_time)
Weekly_Overtime = (
Mon_overtime + Tue_overtime + Wed_overtime + Thur_overtime + Fri_overtime + Sat_overtime + Sun_overtime)
if Weekly_straight_time <= 40:
self.lcdNumber.display(Weekly_straight_time)
else:
self.lcdNumber.display("40")
if Weekly_Overtime > 40:
self.lcdNumber_2.display(Overtime)
elif Weekly_Overtime <= 40:
self.lcdNumber_2.display(Weekly_Overtime)
#self.lcdNumber_3.dispaly(Weekly_double_time)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
首先要解决2个主要问题: 如ellanesc所述,将self.lineEdit_6()更改为self.lineEdit_6.text()
接下来,在CalHour中的if语句上方初始化您的var:
Mon_overtime = 0
Tue_overtime = 0
Wed_overtime = 0
Thur_overtime = 0
Fri_overtime = 0
Sat_overtime = 0
Sun_overtime = 0
Mon_double_time = 0
Tue_double_time = 0
Wed_double_time = 0
Thur_double_time = 0
Fri_double_time = 0
Sat_double_time = 0
Sun_double_time = 0