Skip to content

PyQt 基础 #2

@freelsn

Description

@freelsn

PyQt 最基本的程序

利用 PyQt 创建一个空窗口,这里的代码是任何 PyQt 程序都需要包含的。

'basic_window.py'

import sys

from PyQt5.QtWidgets import QApplication, QWidget


class EmptyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(200, 100, 400, 300)  # 设置窗口的初始位置和大小
        self.setWindowTitle('Empty Window in PyQt')  # 设置窗口标题
        self.show()  # 显示窗口


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = EmptyWindow()
    sys.exit(app.exec())

执行命令 python basic_window.py,将弹出如图所示的窗口。

image

QApplication is responsible for managing the application’s main event loop and widget initialization and finalization. The main event loop is where user interactions in the GUI window, such as clicking on a button, are managed. 它的输入是 list,支持从命令行输入参数,比如执行 python basic_window.py -style Fusion。如果不需要接受命令行参数,可以写成 app=QApplication([])

The method exec() starts the application’s event loop and will remain here until you quit the application. The function sys.exit() ensures a clean exit.

在创建一个窗口时,一般会创建一个 class,并继承 QMainWindowQWidgetQDialog 其中一个。

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions