我 的 个 人 主 页: 失心疯的个人主页
入 门 教 程 推 荐 : Python零基础入门教程合集
虚 拟 环 境 搭 建 : Python项目虚拟环境(超详细讲解)
PyQt5 系 列 教 程: Python GUI(PyQt5)文章合集
Oracle数据库教程: Oracle数据库文章合集
优 质 资 源 下 载 : 资源下载合集
QTDesigner 具体使用
Layoutts # 布局控件
Spacers # 弹簧控件
Buttons # 按钮控件
Item Views(Model-Based) #
Item Widgets(Item-Based) #
Containers # 容器控件
Input Widgets # 输入控件
Display Widgets # 展示控件
setProperty(属性名,属性值)
# 创建一个MyBtn.py文件,代码如下
from PyQt5.Qt import *
class Btn(QPushButton):
def __init__(self, *args, **kwargs):
super(Btn, self).__init__(*args, **kwargs)
print('自定义按钮类')
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
Form.setStyleSheet("")
self.pushButton = Btn(Form)
self.pushButton.setGeometry(QtCore.QRect(100, 70, 121, 91))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
from MyBtn import Btn
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(412, 300)
Form.setStyleSheet("")
self.pushButton = Btn(Form)
self.pushButton.setGeometry(QtCore.QRect(100, 70, 121, 91))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
self.pushButton.clicked.connect(Form.btn_click) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
from MyBtn import Btn
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.btn_click = lambda :print('自定义按钮被点击了')
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Horizontal Policy # 垂直策略
Vertical Policy # 水平策略
# 策略参数
Fixed # 固定尺寸
Minimum # 最小尺寸
Maximum # 最大尺寸
Preferred # 优先分配空间
MinimumEXpanding #
EXpanding # 尽可能占据更多空间
Lgnored
Horizontal Stretch # 垂直拉伸系数
Vertical Stretch # 水平拉伸系数
from PyQt5.uic import loadUi
loadUi(ui文件名, self)
test_login.py
文件) from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('ui文件使用')
self.resize(500, 300)
self.addWidget()
def addWidget(self):
from PyQt5.uic import loadUi
loadUi('login.ui', self)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
test_login.py
文件中,通过self.children()
来看一下window对象有哪些子控件dir(self)
来获取window对象内部有哪些属性可以使用(可以通过这些属性来获取到子控件对象)
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('ui文件使用')
self.resize(500, 300)
self.addWidget()
def addWidget(self):
from PyQt5.uic import loadUi
loadUi('login.ui', self)
# print(self.children())
# print(dir(self))
self.btn_login.clicked.connect(self.btn_click)
def btn_click(self):
account = self.le_username.text()
password = self.le_password.text()
print(account, password)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
# login.py 文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(395, 186)
Form.setStyleSheet("")
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setContentsMargins(0, 0, 0, 8)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
self.widget = QtWidgets.QWidget(Form)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy)
self.widget.setStyleSheet("background-image: url(:/back/images/3.jpg);")
self.widget.setObjectName("widget")
self.verticalLayout.addWidget(self.widget)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label_1 = QtWidgets.QLabel(Form)
self.label_1.setObjectName("label_1")
self.horizontalLayout.addWidget(self.label_1)
self.le_username = QtWidgets.QLineEdit(Form)
self.le_username.setObjectName("le_username")
self.horizontalLayout.addWidget(self.le_username)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem2)
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.le_password = QtWidgets.QLineEdit(Form)
self.le_password.setObjectName("le_password")
self.horizontalLayout_2.addWidget(self.le_password)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem3)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.btn_login = QtWidgets.QPushButton(Form)
self.btn_login.setMinimumSize(QtCore.QSize(270, 30))
self.btn_login.setMaximumSize(QtCore.QSize(270, 30))
self.btn_login.setStyleSheet("")
self.btn_login.setObjectName("btn_login")
self.verticalLayout.addWidget(self.btn_login, 0, QtCore.Qt.AlignHCenter)
self.widget.raise_()
self.btn_login.raise_()
self.label_1.setBuddy(self.le_username)
self.label_2.setBuddy(self.le_password)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label_1.setText(_translate("Form", "账号(&N):"))
self.label_2.setText(_translate("Form", "密码(&P):"))
self.btn_login.setText(_translate("Form", "登录"))
import backgroud_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
# 二进制文件太长,此处省略
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('界面文件加载')
self.resize(500, 300)
self.addWidget()
def addWidget(self):
from login import Ui_Form # 导入模块
ui = Ui_Form() # 通过模块内的类创建对象
ui.setupUi(self) # 调用对象的setupUi方法,并将self传递进去
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
from login import Ui_UserLogin
class Window(QWidget, Ui_UserLogin):
def __init__(self):
super().__init__()
self.resize(500, 300)
self.setupUi(self) # 调用父类的setuoUi方法
self.addWidget()
self.setWindowTitle('用户登录')
icon = QIcon('icon/1.ico')
self.setWindowIcon(icon)
def addWidget(self):
self.btn_login.clicked.connect(self.btn_click)
def btn_click(self):
account = self.le_username.text()
password = self.le_password.text()
print(account, password)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
原理讲解
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 200)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(100, 70, 72, 15))
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "MainWindow窗体"))
self.label.setText(_translate("Form", "标签"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 200)
self.label = QtWidgets.QLabel(Form)
# appMain2.py 多继承方法
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from ui_FormHello import Ui_Form
class QmyWidget(QWidget,Ui_Form):
def __init__(self, parent=None):
super().__init__(parent) # 调用父类构造函数,创建QWidget窗体
self.Lab="多重继承的QmyWidget" # 新定义的一个变量
self.setupUi(self) # self是QWidget窗体,可作为参数传给setupUi()
self.LabHello.setText(self.Lab)
if __name__ == "__main__":
app = QApplication(sys.argv) #创建app
myWidget=QmyWidget()
myWidget.show()
myWidget.btnClose.setText("不关闭了")
sys.exit(app.exec_())
super().__init__(parent)
self.setupUi(self)
self.Lab="多重继承的QmyWidget" # 新定义的一个属性
self.LabHello.setText(self.Lab)
myWidget.btnClose.setText("不关闭了")
self.LabHello.setText(self.Lab)
# appMain.py 单继承方法,能更好地进行界面与逻辑的分离
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from ui_FormHello import Ui_Form
class QmyWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent) # 调用父类构造函数,创建QWidget窗体
self.__ui=Ui_Form() # 创建UI对象
self.__ui.setupUi(self) # 构造UI
self.Lab="单继承的QmyWidget"
self.__ui.LabHello.setText(self.Lab)
def setBtnText(self, aText):
self.__ui.btnClose.setText(aText)
if __name__ == "__main__":
app = QApplication(sys.argv) # 创建app,用QApplication类
myWidget=QmyWidget()
myWidget.show()
myWidget.setBtnText("间接设置")
sys.exit(app.exec_())
self.__ui=Ui_Form() # 创建UI对象
myWidget.setBtnText("间接设置")
self.__ui.LabHello.setText(self.Lab)
self.ui=Ui_FormHello()