PyQt5 – 如何隐藏窗口的标题栏
当我们使用PyQt5设计GUI(图形用户界面)应用程序时,存在着窗口。窗口是计算机显示器上的一个(通常)长方形部分,它所显示的内容(例如,一个目录的内容、一个文本文件或一张图片)似乎独立于屏幕的其他部分。窗口是构成图形用户界面(GUI)的元素之一。
在一个窗口中,我们可以看到有一个标题栏,它包括左边的图标和标题,右边有控制按钮。
在这篇文章中,我们将看到如何隐藏标题栏。为了做到这一点,我们将使用setWindowFlag()方法并传递给QWidget类。
语法: setWindowFlag(Qt.FramelessWindowHint)
参数: 它需要窗口类型作为参数。
执行的动作: 它删除了标题栏。
代码。
# importing the required libraries
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# this will hide the title bar
self.setWindowFlag(Qt.FramelessWindowHint)
# set the title
self.setWindowTitle("no title")
# setting the geometry of window
self.setGeometry(100, 100, 400, 300)
# creating a label widget
# by default label will display at top left corner
self.label_1 = QLabel('no title bar', self)
# moving position
self.label_1.move(100, 100)
# setting up border and background color
self.label_1.setStyleSheet("background-color: lightgreen;
border: 3px solid green")
# show all the widgets
self.show()
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :