简体   繁体   中英

How get a frameless window in pyqt5?

I am new to PyQt5 and am having a hard time figuring out how will I convert the code accordingly so that it is frameless. I have used flags but it keeps giving me an error that the "UI_MainWindow object has no attribute 'SetWindowFlags'. The problem is in the execution part but I'm unable to figure out how to make the alterations.

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainScreen.UI'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QSizeGrip
import sys


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(688, 525)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.Header = QtWidgets.QFrame(self.centralwidget)
        self.Header.setMaximumSize(QtCore.QSize(16777215, 50))
        self.Header.setStyleSheet("background-color: rgb(239, 128, 0);")
        self.Header.setFrameShape(QtWidgets.QFrame.WinPanel)
        self.Header.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Header.setObjectName("Header")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.Header)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setSpacing(0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.Title_bar_container = QtWidgets.QFrame(self.Header)
        self.Title_bar_container.setStyleSheet("background-color: rgb(239, 133, 0);")
        self.Title_bar_container.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.Title_bar_container.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Title_bar_container.setObjectName("Title_bar_container")
        self.Slider = QtWidgets.QFrame(self.Title_bar_container)
        self.Slider.setGeometry(QtCore.QRect(0, 0, 71, 52))
        self.Slider.setStyleSheet("background-color: rgb(239, 133, 0);")
        self.Slider.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.Slider.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Slider.setObjectName("Slider")
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.Slider)
        self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_4.setSpacing(0)
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.pushButton_4 = QtWidgets.QPushButton(self.Slider)
        self.pushButton_4.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_4.setStyleSheet("\n"
"QPushButton{\n"
"background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"}\n"
"\n"
"QPushButton::hover{\n"
"    \n"
"    background-color: rgb(243, 164, 50);\n"
"}")
        self.pushButton_4.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/icon_pics/png/004-list.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_4.setIcon(icon)
        self.pushButton_4.setIconSize(QtCore.QSize(24, 24))
        self.pushButton_4.setObjectName("pushButton_4")
        self.horizontalLayout_4.addWidget(self.pushButton_4)
        self.horizontalLayout_2.addWidget(self.Title_bar_container)
        self.Top_right_buttons = QtWidgets.QFrame(self.Header)
        self.Top_right_buttons.setMaximumSize(QtCore.QSize(100, 16777215))
        self.Top_right_buttons.setStyleSheet("background-color: rgb(239, 133, 0);")
        self.Top_right_buttons.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.Top_right_buttons.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Top_right_buttons.setObjectName("Top_right_buttons")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.Top_right_buttons)
        self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_3.setSpacing(0)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.pushButton = QtWidgets.QPushButton(self.Top_right_buttons)
        self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton.setMouseTracking(False)
        self.pushButton.setWhatsThis("")
        self.pushButton.setStyleSheet("\n"
"QPushButton{\n"
"background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"}\n"
"\n"
"QPushButton::hover{\n"
"    \n"
"    background-color: rgb(243, 164, 50);\n"
"}")
        self.pushButton.setText("")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(":/icon_pics/png/001-minimize.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton.setIcon(icon1)
        self.pushButton.setIconSize(QtCore.QSize(24, 24))
        self.pushButton.setFlat(False)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout_3.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.Top_right_buttons)
        self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_2.setStyleSheet("background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"\n"
"QPushButton::hover{\n"
"    \n"
"    background-color: rgb(243, 164, 50);\n"
"}")
        self.pushButton_2.setText("")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(":/icon_pics/png/003-expand.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_2.setIcon(icon2)
        self.pushButton_2.setIconSize(QtCore.QSize(24, 24))
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_3.addWidget(self.pushButton_2)
        self.pushButton_3 = QtWidgets.QPushButton(self.Top_right_buttons)
        self.pushButton_3.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_3.setStyleSheet("\n"
"QPushButton{\n"
"background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"}\n"
"\n"
"QPushButton::hover{\n"
"    \n"
"    background-color: rgb(243, 164, 50);\n"
"}")
        self.pushButton_3.setText("")
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(":/icon_pics/png/002-remove.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_3.setIcon(icon3)
        self.pushButton_3.setIconSize(QtCore.QSize(24, 24))
        self.pushButton_3.setObjectName("pushButton_3")
        self.horizontalLayout_3.addWidget(self.pushButton_3)
        self.horizontalLayout_2.addWidget(self.Top_right_buttons)
        self.verticalLayout.addWidget(self.Header)
        self.Main_body = QtWidgets.QFrame(self.centralwidget)
        self.Main_body.setStyleSheet("background-color: rgb(0, 0, 127);")
        self.Main_body.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.Main_body.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Main_body.setObjectName("Main_body")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.Main_body)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setSpacing(0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.Left_side_menu = QtWidgets.QFrame(self.Main_body)
        self.Left_side_menu.setMaximumSize(QtCore.QSize(70, 16777215))
        self.Left_side_menu.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
        self.Left_side_menu.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.Left_side_menu.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Left_side_menu.setObjectName("Left_side_menu")
        self.horizontalLayout.addWidget(self.Left_side_menu)
        self.Main_screen = QtWidgets.QFrame(self.Main_body)
        self.Main_screen.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
        self.Main_screen.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.Main_screen.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Main_screen.setObjectName("Main_screen")
        self.horizontalLayout.addWidget(self.Main_screen)
        self.Right_side_menu = QtWidgets.QFrame(self.Main_body)
        self.Right_side_menu.setMaximumSize(QtCore.QSize(70, 16777215))
        self.Right_side_menu.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
        self.Right_side_menu.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.Right_side_menu.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Right_side_menu.setObjectName("Right_side_menu")
        self.horizontalLayout.addWidget(self.Right_side_menu)
        self.verticalLayout.addWidget(self.Main_body)
        self.Footer = QtWidgets.QFrame(self.centralwidget)
        self.Footer.setMaximumSize(QtCore.QSize(16777215, 50))
        self.Footer.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
        self.Footer.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.Footer.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Footer.setObjectName("Footer")
        self.verticalLayout.addWidget(self.Footer)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        flags = QtCore.Qt.WindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
        self.setWindowFlags(flags)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
import Icons_rc


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

The problem comes from the fact that you're trying to edit a file that is not intended to be modified (exactly as the warning in the header says).

setWindowFlags is a function of QWidget classes. The self used there refers to the Ui_MainWindow instance, which is a simple python object (the "Form class").

Theoretically, you can just use that function against the MainWindow argument of setupUi (which is a QWidget subclass, so it implements that function):

    MainWindow.setWindowFlags(flags)

But, no. Don't.
Editing those files is considered bad practice, and trying to do it without knowing what (nor why) you're doing always leads to misunderstandings, unexpected behavior and bugs, just like in your case, not considering the fact that whenever you need to change something in the ui you then have to painfully merge the new ui file with the existing code.

Regenerate that file with pyuic, and leave it as it is . Then create a new script, which will be your actual program script, and do the only thing that is expected with those files: import it.

from PyQt5 import QtWidgets
from file_generated_by_pyuic import Ui_MainWindow

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowFlags(
            self.windowFlags() | 
            QtCore.Qt.FramelessWindowHint | 
            QtCore.Qt.WindowStaysOnTopHint
        )


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    # note the lower case "mainWindow", indicating it's an *instance*
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

In the example above it's implied that you used pyuic to create a file named file_generated_by_pyuic.py .

Read more about this topic in the official guidelines about using Designer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM