PySide文档阅读笔记-第一个Qt for Python程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq78442761/article/details/83787029

想使用Qt for Python要2个条件:
1.Python 3.5以后的版本(包括3.5);
2.libclang 3.9以后的版本(包括3.9);

输入这条命令进行安装;

python -m pip install --index-url=http://download.qt.io/snapshots/ci/pyside/5.11/latest pyside2 --trusted-host download.qt.io

程序运行截图如下:

安装python 使用pip 安装Qt

安装libclang 3.9以后的版本:

他有个选项,是加到环境变量里面,不会配的同学一定要选择,加到环境变量里面;

搞好了,建立一个py文件,测试如下代码:

import PySide2.QtCore

print(PySide2.QtCore.qVersion())

运行截图如下:

再来一套测试代码:

代码如下:

import sys
import random

from PySide2 import QtCore, QtWidgets, QtGui

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

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

        self.button = QtWidgets.QPushButton("Click me!")
        self.text = QtWidgets.QLabel("Hello World")
        self.text.setAlignment(QtCore.Qt.AlignCenter)

        self.text.setFont(QtGui.QFont("Titillium", 30))
        self.button.setFont(QtGui.QFont("Titillium", 20))

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.text)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)

        self.button.clicked.connect(self.magic)


    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_())

运行截图如下:

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/83787029