pyside 官方教程 1 开始

版权声明:本文为博主原创文章,未经博主允许下请随便转载。 https://blog.csdn.net/god_wen/article/details/88076352

Getting Started

_To get started with Qt for Python, install the following prerequisites:

开始编写python 版的qt程序前我们要确保一下几点ok

  • Python 3.5+ or 2.7
  • libclang 5.0+ (for Qt 5.11) or 6.0+ (for Qt 5.12)
  • Recommended: a virtual environment, such as venv or virtualenv

libclang 是存放Qt的dll文件(com),安装对应的qt就ok,venv和virtualenv最新的pycharm都是默认开启的,所以开始qt编程前我们只要安装python,pycharm,qt即可

With these installed, you are ready to install the Qt for Python packages using the pip wheel. Run the following command from your command prompt to install:

开始qt for python 的编写之前,我们要先学会用pip在terminal 上安装PySide2 库,以便后面的import

    pip install PySide2 # For the latest version on PyPi

or:

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

Now that you have Qt for Python installed, you can test your setup by running the following Python constructs to print version information:

你可以通过运行下面这段代码来查看版本信息

import PySide2.QtCore

# Prints PySide2 version
# e.g. 5.11.1a1
print(PySide2.__version__)

# Gets a tuple with each version component
# e.g. (5, 11, 1, 'a', 1)
print(PySide2.__version_info__)

# Prints the Qt version used to compile PySide2
# e.g. "5.11.2"
print(PySide2.QtCore.__version__)

# Gets a tuple with each version components of Qt used to compile PySide2
# e.g. (5, 11, 2)
print(PySide2.QtCore.__version_info__)

Note that the Qt version used to compile Qt for Python may differ from the version used to run Qt for Python. To print the current running Qt version number, you can use:

动用QtCore 是编译时出来的版本,所以可能和直接PySide2出来的版本不同,得到当前运行的版本可以用下列函数


print(PySide2.QtCore.qVersion())

Your Qt for Python setup is ready, so try exploring it further by developing a simple application that prints “Hello World” in several languages. The following instructions will guide you through the development process:

Create a new file named hello_world.py, and add the following imports to it.

准备工作已经完成,现在写一个间的的程序,输出不同语言的“你好世界”。

import sys
import random
from PySide2 import QtCore, QtWidgets, QtGui
The PySide2 Python module provides access to the Qt APIs as its submodule. In this case, you are importing the QtCore, QtWidgets, and QtGui submodules.

Define a class named MyWidget, which extends QWidget and includes a QPushButton and QLabel.

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.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))

The MyWidget class has the magic member function that randomly chooses an item from the list hello. This function is called when you click the button.

MyWidgets 类有一个magic 函数,他可以随机选择hello数组中的元素输出,magic 函数会在点击按钮是被调用

Now, add a main function where you instantiate MyWidget and show it.

现在,添加一个可以是实例化MyWidget类并显示他的入口函数。

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

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

    sys.exit(app.exec_())

Your example is ready to be run. Try clicking the button at the bottom and see which greeting you get.

你编写的小程序已经可以运行了,试试点击按钮看看哪种语言的问候语。

猜你喜欢

转载自blog.csdn.net/god_wen/article/details/88076352