【最终版】PyQt5⾃定义标题栏,实现⽆边框,最⼩化最⼤化关闭事件,窗⼝拖
动移动,窗⼝改变。。。
【最终版】PyQt5 ⾃定义标题栏,实现⽆边框,最⼩化最⼤化关闭事件,窗⼝拖动移动,窗⼝改变⼤⼩,仿百度⽹盘⾊调美化,添加内容窗⼝
⽂章⽬录
前⾔
此篇⽂章是在【】这篇⽂章的基础上改进的。
下⾯⽂章内容如果没有说明,就是和基础版的⼀样。如果说了【改】或【新增】就说明和基础版的不⼀致了。
1.⽂件架构【新增】
|
|-TitleTest.py 标题栏ui代码
|
|-CallTitleTest.py 标题栏逻辑代码
|- - - - -|改变button按钮的形状
|-------- |-textedit_test.py内容窗⼝ui代码
|---------|-Call_textedit_test.py内容窗⼝逻辑代码
。
2.实现效果【增加】
没有内容的标题栏效果
标题栏适应了内容窗⼝⼤⼩。
3. 标题栏Qtdesigner的ui设计(ui设计代码)设计图:
布局
具体控件设置请看 ui的py⽂件
请留意 3 个pushButton和 2 个lable的⼤⼩设计
TitleTest.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'TitleTest.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_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setContentsMargins(0,0,0,0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.widget = QtWidgets.QWidget(Form)
self.widget.setMaximumSize(QtCore.QSize(16777215,35))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0,0,0,0)
self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(5,20, QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label = QtWidgets.QLabel(self.widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(20)
sizePolicy.setVerticalStretch(20)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setMaximumSize(QtCore.QSize(20,20))
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
spacerItem1 = QtWidgets.QSpacerItem(5,20, QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem1)
self.label_2 = QtWidgets.QLabel(self.widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(30)
sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth())
self.label_2.setSizePolicy(sizePolicy)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
spacerItem2 = QtWidgets.QSpacerItem(40,20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem2)
self.pushButton = QtWidgets.QPushButton(self.widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(20)
sizePolicy.setVerticalStretch(20)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setMinimumSize(QtCore.QSize(0,0))
self.pushButton.setMaximumSize(QtCore.QSize(20,20))
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
spacerItem3 = QtWidgets.QSpacerItem(8,20, QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem3)
self.pushButton_2 = QtWidgets.QPushButton(self.widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(20)
sizePolicy.setVerticalStretch(20)
sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth())
self.pushButton_2.setSizePolicy(sizePolicy)
self.pushButton_2.setMaximumSize(QtCore.QSize(20,20))
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
spacerItem4 = QtWidgets.QSpacerItem(8,20, QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem4)
self.pushButton_3 = QtWidgets.QPushButton(self.widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(20)
sizePolicy.setVerticalStretch(20)
sizePolicy.setHeightForWidth(self.pushButton_3.sizePolicy().hasHeightForWidth())
self.pushButton_3.setSizePolicy(sizePolicy)
self.pushButton_3.setMaximumSize(QtCore.QSize(20,20))
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout.addWidget(self.pushButton_3)
spacerItem5 = QtWidgets.QSpacerItem(5,20, QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem5)
self.verticalLayout.addWidget(self.widget)
self.widget_2 = QtWidgets.QWidget(Form)
self.widget_2.setObjectName("widget_2")
self.verticalLayout.addWidget(self.widget_2)
tSlotsByName(Form)
def retranslateUi(self, Form):
_translate = anslate
Form.setWindowTitle(_translate("Form","Form"))
self.label.setText(_translate("Form","图标"))
self.label_2.setText(_translate("Form","TextLabel"))
self.pushButton.setToolTip(_translate("Form","<html><head/><body><p>最⼩化</p></body></html>"))
self.pushButton.setText(_translate("Form","-"))
self.pushButton_2.setToolTip(_translate("Form","<html><head/><body><p>最⼤化</p></body></html>"))
self.pushButton_2.setText(_translate("Form","O"))
self.pushButton_3.setToolTip(_translate("Form","<html><head/><body><p>关闭</p></body></html>"))
self.pushButton_3.setText(_translate("Form","X"))
。
4. 设置标题和图标【改】
注:完整的菜单栏逻辑代码请看最下⾯的CallTitleTest.py⽂件代码
单独设置⼀个函数来整理【标题和图标】设置,如果全部放在__init__初始化函数⾥,会很混乱的。只需在__init__中初始化这个函数即可。
改:把设置⽆边框部分放在QSS美化部分讲。这部分就说【标题和图标】的设置函数。
如果看来基础版的代码,可以发现,我把⽆边框和【标题和图标】分成两个函数了。因为【标题和图标】设置部分做了改变。
⾸先在__init__函数中设置两个参数来接收图标和标题
# 接收图标和标题
self.icon_path = icon_path
self.title = title
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论