天宫鹤

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   GoGrid  阅读(46)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示