You can download this code by clicking the button below.
This code is now available for download.
Create a simple PyQt6 widget based window that contains a label with a random text.
Technology Stack : PyQt6, QWidget, QVBoxLayout, QLabel
Code Type : PyQt6 custom widget
Code Difficulty : Intermediate
import random
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
def random_pyqt6_widget():
class RandomWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()
def initUI(self):
layout = QVBoxLayout()
# 随机选择一个标签文本
label_text = random.choice(["Hello PyQt6", "Welcome to PyQt6", "PyQt6 is awesome"])
label = QLabel(label_text, self)
layout.addWidget(label)
self.setLayout(layout)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Random PyQt6 Widget')
app = QApplication([])
widget = RandomWidget()
widget.show()
app.exec()