PYQT5 透明界面 无标题栏 可以拖动 高级版

At the beginning , I will show you how the code runs and what it looks like. The window has a transparent background with a solid white area inside the frame. This design is intended to provide you with a basic understanding of its functionality. You will need to add other functions to this window according to your own requirements.

Here is the code that you can directly run in your PyCharm or any other platform. Feel free to give it a try. Thank you!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget,QApplication,QMainWindow,QInputDialog
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(661, 490)
        # =========================== # 主要代码
        self.setWindowFlags(QtCore.Qt.WindowType.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        # =========================== #
        self.centralwidget = QtWidgets.QWidget()
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(0, 0, 661, 491))
        self.frame.setStyleSheet("background-color: rgba(33, 151, 230, 110);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.frame_2 = QtWidgets.QFrame(self.frame)
        self.frame_2.setGeometry(QtCore.QRect(10, 10, 231, 471))
        self.frame_2.setStyleSheet("background-color: rgb(255, 251, 252);\n""border-radius:10px;")
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")
        self.setCentralWidget(self.centralwidget)


    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()


    def mouseMoveEvent(self, event):
        delta = QPoint (event.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_MainWindow()
    ui.show()
    sys.exit(app.exec_())

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt5是一款Python的GUI编程工具包,用于创建桌面应用程序。如果你想在PyQt5中自定义标题栏,可以通过以下步骤实现: 1. 创建一个QMainWindow对象,并调用setWindowFlag方法来设置窗口样式,例如: ``` self.setWindowFlag(QtCore.Qt.FramelessWindowHint) ``` 2. 创建一个QWidget对象作为标题栏,并将其添加到QMainWindow对象的布局中。 3. 在标题栏中添加控件,例如QPushButton、QLabel等。 4. 重写QMainWindow对象的mousePressEvent、mouseMoveEvent和mouseReleaseEvent方法,以实现拖动窗口的功能。 下面是一个简单的例子: ```python from PyQt5 import QtCore, QtWidgets class CustomTitleBar(QtWidgets.QWidget): def __init__(self, parent=None): super(CustomTitleBar, self).__init__(parent) self.setParent(parent) self.setFixedHeight(30) layout = QtWidgets.QHBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) self.title = QtWidgets.QLabel(self) self.title.setMargin(5) layout.addWidget(self.title) self.btn_close = QtWidgets.QPushButton(self) self.btn_close.clicked.connect(self.parent().close) layout.addWidget(self.btn_close) layout.addStretch() class MainWindow(QtWidgets.QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setWindowFlag(QtCore.Qt.FramelessWindowHint) self.setStyleSheet("background-color: white;") self.title_bar = CustomTitleBar(self) self.title_bar.title.setText("Custom Title Bar") self.setCentralWidget(QtWidgets.QTextEdit(self)) self.resize(500, 300) def mousePressEvent(self, event): if event.button() == QtCore.Qt.LeftButton: self.dragPos = event.globalPos() - self.frameGeometry().topLeft() event.accept() def mouseMoveEvent(self, event): if event.buttons() == QtCore.Qt.LeftButton: self.move(event.globalPos() - self.dragPos) event.accept() if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) w = MainWindow() w.show() sys.exit(app.exec_()) ``` 这个例子中,我们创建了一个CustomTitleBar类作为标题栏,并将其添加到了QMainWindow对象中。我们还重写了QMainWindow对象的mousePressEvent、mouseMoveEvent和mouseReleaseEvent方法,以实现拖动窗口的功能。 在实际开发中,你可以根据需要自定义标题栏的样式和控件,以及实现更复杂的功能,例如最小化、最大化和还原窗口等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值