1.环境配置
1.1.添加qt库
pip install pyqt5
pip install pyqt5-tools
1.2.Pycharm相关配置:
Arguments:-m PyQt5.uic.pyuic F i l e N a m e FileName FileName -o F i l e N a m e W i t h o u t E x t e n s i o n FileNameWithoutExtension FileNameWithoutExtension.py
Program:C:\Python37\Scripts\pyrcc5.exe
Arguments: F i l e N a m e FileName FileName -o F i l e N a m e W i t h o u t E x t e n s i o n FileNameWithoutExtension FileNameWithoutExtension_rc.py
2.使用QT界面设计
项目->External tool->Qt Designer
3.将ui文件转py文件,进行界面操控
选中生成的ui文件,右键-External tool->PyUIC,然后就会发现生成对应的py文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Clothes_insertData.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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(300, 30, 75, 23))
self.pushButton.setObjectName("pushButton")
self.horizontalLayoutWidget = QtWidgets.QWidget(Dialog)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 20, 261, 41))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Dialog)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(10, 70, 261, 41))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.horizontalLayoutWidget_2)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.horizontalLayoutWidget_3 = QtWidgets.QWidget(Dialog)
self.horizontalLayoutWidget_3.setGeometry(QtCore.QRect(10, 120, 261, 41))
self.horizontalLayoutWidget_3.setObjectName("horizontalLayoutWidget_3")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_3)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_3 = QtWidgets.QLabel(self.horizontalLayoutWidget_3)
self.label_3.setObjectName("label_3")
self.horizontalLayout_3.addWidget(self.label_3)
self.lineEdit_3 = QtWidgets.QLineEdit(self.horizontalLayoutWidget_3)
self.lineEdit_3.setObjectName("lineEdit_3")
self.horizontalLayout_3.addWidget(self.lineEdit_3)
self.horizontalLayoutWidget_4 = QtWidgets.QWidget(Dialog)
self.horizontalLayoutWidget_4.setGeometry(QtCore.QRect(10, 170, 261, 41))
self.horizontalLayoutWidget_4.setObjectName("horizontalLayoutWidget_4")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_4)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_4 = QtWidgets.QLabel(self.horizontalLayoutWidget_4)
self.label_4.setObjectName("label_4")
self.horizontalLayout_4.addWidget(self.label_4)
self.lineEdit_4 = QtWidgets.QLineEdit(self.horizontalLayoutWidget_4)
self.lineEdit_4.setObjectName("lineEdit_4")
self.horizontalLayout_4.addWidget(self.lineEdit_4)
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "手工添加数据"))
self.pushButton.setText(_translate("Dialog", "添加"))
self.label.setText(_translate("Dialog", "客户名字"))
self.label_2.setText(_translate("Dialog", "衣服款式"))
self.label_3.setText(_translate("Dialog", "衣服尺寸"))
self.label_4.setText(_translate("Dialog", "订单件数"))
4.再写一个test.py来启动这个窗口:
#coding=utf-8
import sys
import Clothes_insertData
from PyQt5.QtWidgets import QApplication,QDialog
if __name__ == '__main__':
myapp = QApplication(sys.argv)
myDlg = QDialog()
myUI = Clothes_insertData.Ui_Dialog()
myUI.setupUi(myDlg)
#在屏幕中显示widget
myDlg.show()
#exec是python保留关键字,所以用exec_()来代替
sys.exit(myapp.exec_())
弹出提示框参考:https://www.cnblogs.com/zhoug2020/p/10094855.html
窗体如何弹出子窗体:
#coding=utf-8
import sys
import os
if hasattr(sys, 'frozen'):
os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
from Colthes_main import *
from Clothes_insertData import *
from PyQt5.QtWidgets import QApplication,QMainWindow,QDialog,QTableWidgetItem,QMessageBox
from PyQt5.QtGui import QFont
from sqlHelper import SqlHelper
from PyQt5 import QtCore
#主窗口
class parrentWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.main_ui = Ui_MainWindow()
self.main_ui.setupUi(self)
#添加事件监听
self.click_method()
self.result = []
# 表头
self.horizontalHeader = ["客户名字", "衣服款式", "衣服尺寸", "订单件数","电话","地址"]
# 初始化表格
self.table()
#子窗口
class childWindow(QDialog):
def __init__(self):
QDialog.__init__(self)
self.child_ui =Ui_Dialog()
self.child_ui.setupUi(self)
#添加监听事件
self.click_method()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = parrentWindow()
child = childWindow()
#通过toolButton将两个窗体连接
btn = window.main_ui.pushButton_2
btn.clicked.connect(child.show)
#显示
window.show()
sys.exit(app.exec_())