In this post want to show you How To Create Frameless Window In PyQt5, also we are going to use QSizeGrip for resizing the Frameless Window In PyQt5. the QSizeGrip class provides a resize handle for resizing top-level windows. this widget works like the standard Windows resize handle.
What is Frameless Window In PyQt5 ?
frameless window in PyQt5 is a window widget that does not have the usual window decorations such as a title bar, minimize/maximize buttons, and borders. instead it provides completely customizable and clean interface that allows developers to design windows with unique shapes, sizes, and styles.
with frameless window developers have complete control over the window’s appearance and behavior. They can create custom title bars and buttons, add transparency effects, and create non-rectangular windows with rounded corners, irregular shapes, and so on.
To create a frameless window in PyQt5, you can use the setWindowFlags method of the QWidget class and set the Qt.FramelessWindowHint flag. This flag indicates that the window should have no frame and no decorations. You can then customize the window using the setStyleSheet method to apply styles, colors, and other visual effects.
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First we need some imports.
1 2 3 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QSizeGrip import sys |
After that we are going to create our main Window class that extends from QWidget. and in the constructor of the class we need to add some requirements of the window like set window title, window icon and window geometry.
1 2 3 4 5 6 7 8 9 10 11 |
class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 Size Grip" self.top = 200 self.left = 500 self.width = 640 self.height = 480 self.setWindowTitle(self.title) self.setWindowIcon(QtGui.QIcon("icon.png")) self.setGeometry(self.left, self.top, self.width, self.height) |
In here we are going to set the flags for our window. and we using frame less window.
1 2 |
flags = QtCore.Qt.WindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint) self.setWindowFlags(flags) |
Also every PyQt5 application must create an application object.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec()) |
Run the complete code and this will be the result.
Complete source code for How To Create Frameless Window In PyQt5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QSizeGrip import sys class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 Size Grip" self.top = 200 self.left = 500 self.width = 640 self.height = 480 self.setWindowTitle(self.title) self.setWindowIcon(QtGui.QIcon("icon.png")) self.setGeometry(self.left, self.top, self.width, self.height) flags = QtCore.Qt.WindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint) self.setWindowFlags(flags) vboxlayout = QVBoxLayout() sizegrip = QSizeGrip(self) #sizegrip.setVisible(True) vboxlayout.addWidget(sizegrip) self.setLayout(vboxlayout) self.show() if __name__ == "__main__": App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) |
This is another example for PyQt5 Frameless Window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
from PyQt5.QtCore import Qt from PyQt5.QtGui import QPainter, QColor from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel class FramelessWindow(QWidget): def __init__(self): super().__init__() # Set window properties self.setWindowFlags(Qt.FramelessWindowHint) self.setAttribute(Qt.WA_TranslucentBackground) # Create custom title bar self.title_bar = QWidget(self) self.title_bar.setGeometry(0, 0, 400, 30) self.title_bar.setStyleSheet('background-color: #303030;') # Add title label self.title_label = QLabel('My Frameless Window', self.title_bar) self.title_label.move(10, 5) self.title_label.setStyleSheet('color: white;') # Add close button self.close_button = QPushButton('X', self.title_bar) self.close_button.setGeometry(370, 5, 20, 20) self.close_button.setStyleSheet('background-color: none; color: white; border: none;') self.close_button.clicked.connect(self.close) # Add content self.content = QLabel('Codeloop', self) self.content.move(50, 50) self.setGeometry(300, 300, 400, 200) def paintEvent(self, event): # Draw custom border painter = QPainter(self) painter.setPen(QColor(0, 0, 0, 0)) painter.setBrush(QColor(255, 255, 255, 50)) painter.drawRoundedRect(0, 0, self.width() - 1, self.height() - 1, 10, 10) if __name__ == '__main__': app = QApplication([]) win = FramelessWindow() win.show() app.exec_() |
This code creates simple window with custom title bar, close button and some content. it also adds custom border with rounded corners to the window using the paintEvent method. you can modify this code to add more customizations and effects to your PyQt5 frameless windows.
Run the code this will be the result
Subscribe and Get Free Video Courses & Articles in your Email