0%

PyQt5入门:为Python小工具开发GUI界面

1. pyqt5及qtdesigner的安装

1.1 安装

安装步骤网上一堆自行谷歌

1.2 使用pyqt5生成ui文件后无法转py,报错

错误如下:

Fatal error in launcher: Unable to create process using ‘“‘

原因:

执行语句错误,完整应为

“python -m PyQt5.uic.pyuic untitled.ui -o untitled.py”

注意在pycharm中的tools中的pyuic的settings—External Tools—(External Tools)Pyuic内Tools settings设置的正确,正确如下

1
2
3
Program: D:\Anaconda3\envs\python36\python.exe
Arguments: -m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Working directory: $FileDir$

2. 使用

功能比较简单,就是一个根据时间和工作日/休息日的不同计算出差补助。

主要是因为之前没用过pyqt5,所以突发奇想尝试一下,发现也是蛮简单的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.

from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtWidgets
from datetime import datetime
import datetime as datetime0
# 添加这一行可以在程序捕获异常的时候pyqt不崩溃
import cgitb
cgitb.enable( format = 'text')

class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(586, 401)
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(130, 100, 141, 41))
self.textEdit.setObjectName("textEdit")
self.textEdit_2 = QtWidgets.QTextEdit(Form)
self.textEdit_2.setGeometry(QtCore.QRect(420, 100, 121, 41))
self.textEdit_2.setObjectName("textEdit_2")
self.textEdit_4 = QtWidgets.QTextEdit(Form)
self.textEdit_4.setGeometry(QtCore.QRect(420, 190, 121, 41))
self.textEdit_4.setObjectName("textEdit_4")
self.textEdit_5 = QtWidgets.QTextEdit(Form)
self.textEdit_5.setGeometry(QtCore.QRect(210, 270, 281, 91))
self.textEdit_5.setObjectName("textEdit_5")
self.textEdit_5.setReadOnly(True)
self.textEdit_6 = QtWidgets.QTextEdit(Form)
self.textEdit_6.setGeometry(QtCore.QRect(130, 190, 141, 41))
self.textEdit_6.setObjectName("textEdit_6")

self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(110, 290, 61, 41))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(40, 100, 71, 31))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(320, 100, 81, 31))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(30, 190, 91, 31))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(320, 200, 91, 16))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(Form)
self.label_5.setGeometry(QtCore.QRect(40, 40, 281, 41))
self.label_5.setObjectName("label_5")

self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
Form.show()

def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.textEdit.setHtml(_translate("Form",
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
self.pushButton.setText(_translate("Form", "计算"))
self.label.setText(_translate("Form", "起始时间:"))
self.label_2.setText(_translate("Form", "结束时间:"))
self.label_3.setText(_translate("Form", "工作日价格:"))
self.label_4.setText(_translate("Form", "休息日价格:"))
self.label_5.setText(_translate("Form", "时间输入格式:20200101"))
self.textEdit_2.setHtml(_translate("Form",
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
self.textEdit_4.setHtml(_translate("Form",
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
self.textEdit_6.setHtml(_translate("Form",
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))

self.pushButton.clicked.connect(self.calc)

def calc(self):

try:
startTime = self.textEdit.toPlainText()
stopTime = self.textEdit_2.toPlainText()
holiday_price = self.textEdit_4.toPlainText()
workday_price = self.textEdit_6.toPlainText()
# today = datetime.now().weekday() + 1
工作日 = 0
休息日 = 0
begin = datetime0.date(int(startTime[0:4]), int(startTime[4:6]), int(startTime[6:8]))
end = datetime0.date(int(stopTime[0:4]), int(stopTime[4:6]), int(stopTime[6:8]))
for eachDay in range((end - begin).days + 1):
eachDay = str(begin + datetime0.timedelta(days=eachDay)).split("-")

week = datetime.strptime(eachDay[0] + eachDay[1] + eachDay[2], "%Y%m%d").weekday() + 1
if week > 0 and week < 6:
工作日 += 1
else:
休息日 += 1
result = "一共有工作日{}天,休息日{}天\n".format(工作日, 休息日) +\
"出差补贴一共为{}元".format(工作日 * int(workday_price) + 休息日 * int(holiday_price))
self.textEdit_5.setText(result)
except Exception :
# 输入框制空并显示重新输入
self.textEdit_5.setText("输入格式有误\n请检查并重新输入")
# self.textEdit.setText()
# self.textEdit_2.setText()
# self.textEdit_5.setText()
# self.textEdit_6.setText()


if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
widget = QWidget(None)
ui = Ui_Form()
ui.setupUi(widget)
sys.exit(app.exec_())
pass