龙空技术网

Python—使用PySide2(Qt for Python)编写跨平台GUI程序

碳基科技 1661

前言:

现在兄弟们对“qt与python混合编程”大致比较讲究,各位老铁们都想要了解一些“qt与python混合编程”的相关资讯。那么小编同时在网络上收集了一些有关“qt与python混合编程””的相关内容,希望看官们能喜欢,看官们快快来学习一下吧!

PySide2是Qt官方出品,18年正式开始发布。PyQt是以前Nokia推出的。

这里介绍PySide2快速编写跨平台GUI Demo。

安装

目前最新的是5.14版本

pip install pyside2

其安装两个包:

pyside2: contains all the PySide2 module for all the Qt libraries. Also depends on the shiboken2 module.

shiboken2: contains the shiboken2 module with helper functions for PySide2.

代码

# -*- coding: utf-8 -*-

import sys

import random

from PySide2.QtWidgets import (QApplication, QLabel, QPushButton,

QVBoxLayout, QWidget)

from PySide2.QtCore import Slot, Qt

class MyWidget(QWidget):

def __init__(self):

QWidget.__init__(self)

self.hello = ["Hallo Welt", "你好,世界", "Hei maailma",

"Hola Mundo", "Привет мир"]

self.button = QPushButton("Click me!")

self.text = QLabel("Hello World")

self.text.setAlignment(Qt.AlignCenter)

self.layout = QVBoxLayout()

self.layout.addWidget(self.text)

self.layout.addWidget(self.button)

self.setLayout(self.layout)

# Connecting the signal

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

@Slot()

def magic(self):

self.text.setText('\n'.join(self.hello))

if __name__ == "__main__":

app = QApplication(sys.argv)

widget = MyWidget()

widget.resize(800, 600)

widget.show()

sys.exit(app.exec_())

标签: #qt与python混合编程