PyQt5界面设计
效果图
源代码
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout, QPushButton
from PyQt5.QtGui import QPixmap
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置窗口标题和大小
self.setWindowTitle('QLabel clear() Example')
self.setGeometry(100, 100, 400, 300)
# 创建布局
layout = QVBoxLayout()
# 创建 QLabel 并设置初始文本
self.label = QLabel("Initial Text", self)
layout.addWidget(self.label)
# 创建按钮以触发清除操作
clear_button = QPushButton("Clear Label", self)
clear_button.clicked.connect(self.clear_label)
layout.addWidget(clear_button)
# 创建另一个按钮以重新设置文本
set_text_button = QPushButton("Set New Text", self)
set_text_button.clicked.connect(self.set_new_text)
layout.addWidget(set_text_button)
# 创建一个按钮以设置图像
set_image_button = QPushButton("Set Image", self)
set_image_button.clicked.connect(self.set_image)
layout.addWidget(set_image_button)
# 设置主窗口的布局
self.setLayout(layout)
def clear_label(self):
"""清除 QLabel 的内容"""
self.label.clear()
def set_new_text(self):
"""设置 QLabel 的新文本"""
self.label.setText("New Text Content")
def set_image(self):
"""设置 QLabel 的图像"""
pixmap = QPixmap("D:\WPS Office\cartoon_nezha.jpg") # 替换为你的图像路径
self.label.setPixmap(pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())