In this PyQt5 article i want to show you how to Create Label & Stylesheets With QLabel In PyQt5. the QLabel widget provides a text or image display. QLabel is used for displaying text or an image.No user interaction functionality is provided.
What is PyQt5 QLabel ?
In PyQt5 QLabel is widget class that provides display area for text or image. it can be used to display text message, an image or both. QLabel can also be used as hyperlink or tool tip. QLabel is part of the QtWidgets module in PyQt5 and is subclass of the QFrame widget. it has different properties and methods that can be used to customize its appearance and behavior. Some of the common properties include:
- text: sets or gets the text displayed by the label
- pixmap: sets or gets the image displayed by the label
- alignment: sets the alignment of the text or image within the label
- wordWrap: sets whether the text should be automatically wrapped to fit within the label
- sizeHint: returns the recommended size of the label based on its contents
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Psyide2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First we need some imports
1 2 3 |
from PyQt5.QtWidgets import QApplication, QLabel, QDialog, QVBoxLayout import sys from PyQt5 import QtGui |
After that we are going to create our main Window class that extends from QDialog . and in the constructor of the class we need to initialize some requirements of the window . also we have called our initWindow() method in here.
1 2 3 4 5 6 7 8 9 10 |
class Window(QDialog): def __init__(self): super().__init__() self.title = "PyQt5 Labels" self.top = 200 self.left = 400 self.width = 400 self.height = 100 self.iconName = "icon.png" self.InitWindow() |
After that we are going to create our InitWindow() method. we set our window title, window icon and window geometry.
1 2 3 4 |
def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) |
In here we have created QVBoxLayout object with QLabel, and we have added the label to the layout. also we have set the font for the QLabel.
1 2 3 4 5 |
vbox = QVBoxLayout() label = QLabel("This Is PyQt5 Labels") vbox.addWidget(label) label2 = QLabel("This Is PyQt5 GUI Applicaition Development, Hello") label2.setFont(QtGui.QFont("Sanserif", 20)) |
This is used for giving color for our label.
1 |
label2.setStyleSheet('color:red') |
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()) |
Complete source code for PyQt5 Create Label & Stylesheets With QLabel
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 |
from PyQt5.QtWidgets import QApplication, QLabel, QDialog, QVBoxLayout import sys from PyQt5 import QtGui class Window(QDialog): def __init__(self): super().__init__() self.title = "PyQt5 Labels" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) vbox = QVBoxLayout() label = QLabel("This Is PyQt5 Labels") vbox.addWidget(label) label2 = QLabel("This Is PyQt5 GUI Applicaition Development, Hello") label2.setFont(QtGui.QFont("Sanserif", 20)) label2.setStyleSheet('color:red') vbox.addWidget(label2) self.setLayout(vbox) self.show() if __name__ == "__main__": App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) |
Run the complete code and this is the result
PyQt5 Interactive QLabel Widgets
In this section we want to focus on creating QLabel widgets with interactive features such as hyperlinks and tooltips in PyQt5. QLabel is a widget class in PyQt5 used for displaying text or images, and it can be enhanced to support user interaction through signals and slots.
This is an example
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 |
import sys from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout from PyQt5.QtCore import Qt class InteractiveLabelExample(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Interactive QLabel Example") self.setGeometry(100, 100, 400, 200) layout = QVBoxLayout() # Create a clickable QLabel acting as a hyperlink hyperlink_label = QLabel("<a href='https://codeloop.org'>Click Here for Example</a>") hyperlink_label.setOpenExternalLinks(True) # Open link in a web browser hyperlink_label.setFixedWidth(200) layout.addWidget(hyperlink_label) # Create a QLabel with a tooltip tooltip_label = QLabel("Hover Over Me for Tooltip") tooltip_label.setToolTip("This is a tooltip") layout.addWidget(tooltip_label) self.setLayout(layout) # Connect QLabel click event to a custom slot hyperlink_label.linkActivated.connect(self.open_url) def open_url(self, url): # Custom slot to handle QLabel click event (link activation) print("Opening URL:", url) # You could use QDesktopServices.openUrl(QUrl(url)) # to open the URL in a web browser if __name__ == "__main__": app = QApplication(sys.argv) window = InteractiveLabelExample() window.show() sys.exit(app.exec_()) |
The code creates a PyQt5 application with a QWidget containing two QLabel widgets. One QLabel acts as a clickable hyperlink, while the other QLabel displays a tooltip when hovered over.
Run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email