天天看点

【PyQt5】{0} —— QLabel 和 QPushButton

QLabel

在 PyQt5 中,QLabel 部件提供文本或图像显示:

# -*- coding: utf-8 -*-
"""
Created on Fri May  8 21:52:56 2020

@author: Giyn
"""

import sys
from PyQt5.QtWidgets import QApplication, QLabel


if __name__ == "__main__":
    app = QApplication(sys.argv) # 实例化一个QApplication对象
    label = QLabel("This is a Label") # 实例化一个QLabel文本控件
    # label.setText("This is a Label") # 参数可以是HTML代码
    label.show() # 窗口和空间默认都是隐藏的,所以需要show方法进行显示
    sys.exit(app.exec()) # app.exec方法返回0
           

Output:

【PyQt5】{0} —— QLabel 和 QPushButton

QPushButton

在 PyQt 中,QPushButton 部件提供了一个命令按钮:

# -*- coding: utf-8 -*-
"""
Created on Fri May  8 21:52:56 2020

@author: Giyn
"""

import sys
from PyQt5.QtWidgets import QApplication, QPushButton


if __name__ == "__main__":
    app = QApplication(sys.argv) # 实例化一个QApplication对象
    button = QPushButton("I am a Button") # 实例化一个QPushButton对象
    # button.setText("I am a Button")
    button.show() # 窗口和空间默认都是隐藏的,所以需要show方法进行显示
    sys.exit(app.exec()) # app.exec方法返回0
           

Output:

【PyQt5】{0} —— QLabel 和 QPushButton