Mastering QFrame in PyQt5: From Basics to Advanced UI Effects
This article introduces PyQt5's QFrame widget, explains its core properties and methods, demonstrates basic and advanced usage with code examples, and provides guidance on styling, shadows, line widths, and layout integration to create rich desktop GUI interfaces.
QFrame Overview
QFrame is a subclass of QWidget in PyQt5 that allows developers to control border styles, shadows, and shapes, enabling the creation of complex visual effects in desktop applications.
Key methods include setFrameStyle(int style), setFrameShadow(Shadow shadow), setFrameShape(Shape shape), setLineWidth(int width), and setMidLineWidth(int width).
Basic Usage
Import the required modules, create a QFrame instance, and set its shape and shadow.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFrame, QVBoxLayout
class FrameExample(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
vbox = QVBoxLayout()
frame1 = QFrame(self)
frame1.setFrameShape(QFrame.StyledPanel)
frame2 = QFrame(self)
frame2.setFrameShape(QFrame.Box)
vbox.addWidget(frame1)
vbox.addWidget(frame2)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QFrame Example')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FrameExample()
sys.exit(app.exec_())The resulting window shows two frames with different border styles.
Combining QFrame with Other Widgets
QFrame can host other controls to build interactive interfaces.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFrame, QVBoxLayout, QPushButton, QLabel
class AdvancedFrameExample(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
vbox = QVBoxLayout()
frame = QFrame(self)
frame.setFrameShape(QFrame.StyledPanel)
button = QPushButton('Click me', self)
label = QLabel('Hello, QFrame!', self)
vbox.addWidget(frame)
vbox.addWidget(label)
vbox.addWidget(button)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Advanced QFrame Example')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = AdvancedFrameExample()
sys.exit(app.exec_())This example places a button and a label inside a QFrame, demonstrating simple interaction.
Styling and Effects
QFrame supports various border shapes ( QFrame.NoFrame, QFrame.Box, QFrame.Panel, QFrame.StyledPanel, QFrame.HLine, QFrame.VLine, QFrame.WinPanel) and shadow styles ( QFrame.Plain, QFrame.Raised, QFrame.Sunken). Line width can be adjusted with setLineWidth and setMidLineWidth.
Advanced Usage
Beyond basic styling, QFrame can be combined with style sheets, animation frameworks, and layout managers for sophisticated UI designs. Example code shows how to set a custom frame style, line widths, and combine shadow effects:
# Import packages
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('QFrame Feature Test')
window.resize(500, 500)
frame = QFrame(window)
frame.resize(100, 100)
frame.move(100, 100)
frame.setStyleSheet('background-color: cyan;')
# Set combined style and shadow
frame.setFrameStyle(QFrame.Box | QFrame.Raised)
frame.setLineWidth(6)
frame.setMidLineWidth(10)
print(frame.frameWidth())
window.show()
sys.exit(app.exec_())Best Practices and Tips
Use QFrame primarily for visual framing; embed other widgets (e.g., QLabel, QPushButton) to display content.
Consider OS themes and GUI styles when choosing border shapes and shadows.
Ensure style sheet syntax is correct and compatible with the current theme.
Conclusion
QFrame is a powerful PyQt5 widget that offers diverse border and shadow options, enabling developers to craft rich desktop interfaces. By combining QFrame with other PyQt5 features and proper layout management, complex and visually appealing applications can be built efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
