In this PyQt5 article i want to show you How To Create Frame In PyQt5 (QFrame) with
QFrame class. also i want to show you how you can give stylesheet to your PyQt5 Window
and QFrame . The QFrame class is the base class of widgets that can have a frame. QMenu
uses this to “raise” the menu above the surrounding screen. QProgressBar has a “sunken”
look. QLabel has a flat look. The frames of widgets like these can be changed.
Also you can read more Python GUI articles in the below links
1: Kivy GUI Development Tutorials
2: TKinter GUI Development Tutorials
4: wxPython GUI Development Tutorials
5: PyQt5 GUI Development Tutorials
First we need some imports
|
1 2 3 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QFrame, QHBoxLayout, QPushButton 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 Frame" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) |
You can change the color of your window by this code.
|
1 |
self.setStyleSheet('background-color:brown') |
In here we create QPushButton object, also we want to set styleSheet fro our button.
|
1 2 3 |
btn1 = QPushButton( "Click Me") btn1.setStyleSheet("color:white") btn1.setStyleSheet("background-color:green") |
This is for creating of our QFrame.
|
1 2 3 |
frame =QFrame(self) frame.setFrameShape(QFrame.StyledPanel) frame.setLineWidth(0.6) |
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 Frame In PyQt5 (QFrame)
|
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 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QFrame, QHBoxLayout, QPushButton import sys class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 Frame" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.setStyleSheet('background-color:brown') hbox = QHBoxLayout() btn1 = QPushButton( "Click Me") btn1.setStyleSheet("color:white") btn1.setStyleSheet("background-color:green") frame =QFrame(self) frame.setFrameShape(QFrame.StyledPanel) frame.setLineWidth(0.6) hbox.addWidget(frame) hbox.addWidget(btn1) self.setLayout(hbox) self.show() App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) |
Also you can watch the complete video for this article
