天宫鹤

Python使用PyCharm创建一个简单的Qt Widgets应用程序-hello_world.py

"""
Create a Simple Qt Widgets Application
"""
import random
import sys

from PySide6 import QtCore, QtWidgets


# Main Class
class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.hello = ["Hallo Welt", "Hei maailma", "Hola Mundo", "Привет мир", "您好,世界!"]

        self.text = QtWidgets.QLabel()  # 创建部件:标签
        self.text.setText("Hello World")
        self.text.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
        self.button = QtWidgets.QPushButton("Click me!")  # 创建部件:按钮

        self.layout = QtWidgets.QVBoxLayout(self)  # 创建垂直布局
        self.layout.addWidget(self.text)  # 在布局中添加部件:标签
        self.layout.addWidget(self.button)  # 在布局中添加部件:按钮

        self.button.clicked.connect(self.magic)  # 将按钮的clicked信号连接到槽函数magic

    @QtCore.Slot()
    def magic(self):
        self.text.setText(random.choice(self.hello))


if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

    sys.exit(app.exec())

 

posted on 2024-08-11 07:52  GoGrid  阅读(34)  评论(0编辑  收藏  举报

导航