整体思路为
1.使用qt designer 先建立GUI界面,保存
2得到一个**.ui 文件,然后使用pyuic进行转换,得到**.py文件,其中有信号等控制按键,注意换为self等操作
3加入主要的操作 main,此处注意import 几个库
4转换为exe 文件时,将该文件直接右键点击pyinstaller既可
https://www.cnblogs.com/tkinter/p/5632245.html
https://www.cnblogs.com/zhoug2020/p/9039993.html
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'version1.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(471, 384)
Dialog.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(100, 90, 51, 20))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setItalic(False)
font.setUnderline(True)
font.setWeight(75)
font.setKerning(True)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(100, 170, 51, 16))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setUnderline(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.label_2.setFocusPolicy(QtCore.Qt.NoFocus)
self.label_2.setObjectName("label_2")
self.Input1 = QtWidgets.QLineEdit(Dialog)
self.Input1.setGeometry(QtCore.QRect(270, 90, 113, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.Input1.setFont(font)
self.Input1.setObjectName("Input1")
self.Input2 = QtWidgets.QLineEdit(Dialog)
self.Input2.setGeometry(QtCore.QRect(270, 170, 113, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.Input2.setFont(font)
self.Input2.setObjectName("Input2")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(170, 250, 101, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setWeight(50)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
self.pushButton.clicked.connect(self.firstPyQt5_button_click)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def qin(self, a, b):
year = 2017
month = {1: 31, 3: 31, 5: 31, 7: 31, 8: 31, 10: 31, 12: 31, 4: 30, 6: 30, 9: 30, 11: 30}
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
month[2] = 29
else:
month[2] = 28
m = a + 1
n = b + 34 - month[a]
if a == 1 and n > month[2]:
n = n - month[2]
m = m + 1
return m, n
def firstPyQt5_button_click(self, Dialog):
try:
mon = self.Input1.text() #输入数字
d = self.Input2.text() #输入数字2
m, n = self.qin(int(mon), int(d))
# m = str(l[0])
# n = str(l[1])
sh = str(m) + ' 月 ' + str(n) + ' 号'
QtWidgets.QMessageBox.information(self.pushButton,"Result", "Next Time: " + sh)
# self.result_day.setText(n)
except:
print(sys.exc_info())
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog1"))
self.label.setText(_translate("Dialog", "2Month222"))
self.label_2.setText(_translate("Dialog", "Day"))
self.pushButton.setText(_translate("Dialog", "Calcualte"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Dialog()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
qtdesigner 程序 输出后的
此处要注意自己选择的是mian window dialog 还是widget 等等 这对于后面程序有影响
转换完的程序会是mainwindow.slot 这个只适用于自带的操作,比如close等等,
如果自己带入的需要改为 self.slot1
https://blog.csdn.net/justinaelx/article/details/80158307
pyinstaller 安装使用
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
头文件 别少了 这几都加上
import sys
import jisuan #整个程序放在哪里就在哪里
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = jisuan.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
这个是总的程序