Python Qt5環境搭建 (即詳細又全面)

1.下載Python

  1. 首先去Python官網下載需要的版本,官網連接:https://www.python.org/

小插曲:

具體下載哪個版本,我踩了好多坑,此處分享一下踩坑經歷。本人電腦安裝的是Qt5.12.3。由於沒接觸過python,因此第一次去官網下載,肯定下載最新的。安裝完成之後,又去Qt官網下載PySide2xxx,但是怎麼安裝都是失敗,經過不斷的踩坑,推薦一種下載方式。具體如下:

首先去qt官網,查看你要下載的PySide2對應的Python版本,連接:

清華大學開源軟件鏡像站https://mirrors.tuna.tsinghua.edu.cn/qt/official_releases/QtForPython/pyside2/

然後搜索你對應的Qt版本:

 

 

由上圖可以看出,我需要下載pythone的版本爲35~37,因此我下載pythone37.

 

 

2.安裝Python

2.1雙擊安裝文件

Customize installation:自定義 安裝

install launcher for all users:安裝 啓動器 給所有的用戶

Add Python 3.7 to Path: 添加Python到Path環境變量中(勾選)

2.2 Optional features

Documentation:使用手冊

pip:python package indexpython第三方插件管理

tcl/tk and IDLE:python界面操作

Python test suite:python 測試環境

Py lanucher :python啓動器

for all users:給所有用戶

每個都默認勾選

2.3.高級選項

Install for all users :勾選 給所有用戶

Associate files with python:關聯python文件

create shortcuts for installed applications:給安裝的軟件,添加個快捷方式

add python to enviroment variables:將python添加到環境變量中

Precompile standard library:預編譯[準備]python標準文件

Customize install location:本地安裝位置

最後兩個選項不需要勾選,其餘的都勾選。

然後就可以點擊安裝了。

 

2.4.驗證安裝

驗證方式如下:

C:\Users\lsyai\Desktop>pip -V
pip 19.2.3 from d:\program files\python\python37\lib\site-packages\pip (python 3.7)

C:\Users\lsyai\Desktop>python
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print ("hello world")
hello world

2.5.安裝PySide2-xxx

C:\Users\lsyai\Desktop>pip install PySide2-5.12.3-5.12.3-cp35.cp36.cp37-none-win_amd64.whl
Processing c:\users\lsyai\desktop\pyside2-5.12.3-5.12.3-cp35.cp36.cp37-none-win_amd64.whl
Collecting shiboken2==5.12.3 (from PySide2==5.12.3)
  Downloading https://files.pythonhosted.org/packages/53/8a/24c49a3ff59f4a7fe2c9a6737b906f67225ef80ecd8258a8dd54a4dab2f1/shiboken2-5.12.3-5.12.3-cp35.cp36.cp37-none-win_amd64.whl (2.1MB)
     |████████████████████████████████| 2.1MB 50kB/s
Installing collected packages: shiboken2, PySide2
Successfully installed PySide2-5.12.3 shiboken2-5.12.3
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

驗證PySide2-xxx是否安裝成功

新建如下文件:test.py

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(random.choice(self.hello))

if __name__ == "__main__":
    app = QApplication(sys.argv)

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

    sys.exit(app.exec_())

運行:

C:\Users\lsyai\Desktop>python test.py

結果:

 

3.配置Qt Creator(Win10下)

3.1 幫助>關於插件>選中LanguageClient插件

 

關閉QtCreator並再次打開。

3.2 工具>選項>切換到Language Client選項卡,點擊Add按鈕

 

3.3.使用Qtcreator新建一個pythone工程並運行

 

開啓學pythoneQt之路。

 

參考連接:

https://blog.csdn.net/nianmumu/article/details/95522987

https://blog.csdn.net/zscjob/article/details/85337074

https://blog.csdn.net/l1216766050/article/details/84931552

 

下載連接:

https://www.python.org/downloads/release/python-377/

https://mirrors.tuna.tsinghua.edu.cn/qt/official_releases/QtForPython/pyside2/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章