Python installed base of PyQt5

My computer environment: Win10 + Python3.6.4 + JetBrains PyCharm 2017.3.2 x64

Before writing interface with tkinter, now learn how to write interfaces with PyQt5.

 

Installation PyQt5: https://blog.csdn.net/qq_39173907/article/details/79676412

1. Go to the Python installation directory, locate the folder Script, switching paths go in, open cmd window

pip install PyQt5

2. Installation tools

pip install PyQt5-tools

3. Set Environment Variables

On the desktop "My Computer", right mouse button and select "Properties", select "Advanced System Settings" opens, you can see the bottom there is a "Environment Variables" and then add an environment variable in the "System Variables" below.

Variable name: QT_QPA_PLATFORM_PLUGIN_PATH

Path:% Python installation path% \ Lib \ site-packages \ PyQt5 \ Qt \ plugins

 

4. Configuration Pycharm

Open Pycharm, click File -> Setting -> Tools -> External Tools

Originally part of the right side is blank, click on the "+" sign, set as follows:

Program: designer.exe is the path where the path may not be stored in different versions of the same, have to find their own.

  Some% Python installation path% \ Lib \ site-packages \ pyqt5_tools \ designer.exe.

  % Python installation path of some% \ Lib \ site-packages \ pyqt5_tools \ Qt \ bin \ designer.exe

Working directory will fill in "$ FileDir $", more convenient can also click on the right Insert Macro ..., then locate the "$ FileDir $".

This tool can be used to write the UI, the UI below to set up a file becomes py.

 

Program: is the installation path of Python

Arguments:-m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py

Working directory:“$FileDir$”

 

到此为止,设置就完成了。那么我们来简单测试一下可不可以使用。

 

写一个UI

第一步,在你需要写UI的文件路径下右击选择External Tools-->Designer

第二步,画图

第三步,保存画的图到需要写UI的文件路径,取名为firstpic.ui。接着将ui转换成py文件,右击firstpic.ui-->External Tools-->PyUIC

就会在当前目录产生一个firstpic.py文件,文件内容如下:

firstpic.py

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

# Form implementation generated from reading ui file 'firstpic.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1125, 877)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.listView = QtWidgets.QListView(self.centralwidget)
        self.listView.setGeometry(QtCore.QRect(30, 10, 256, 192))
        self.listView.setObjectName("listView")
        self.treeView = QtWidgets.QTreeView(self.centralwidget)
        self.treeView.setGeometry(QtCore.QRect(350, 10, 256, 192))
        self.treeView.setObjectName("treeView")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(60, 240, 551, 411))
        self.groupBox.setObjectName("groupBox")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(40, 30, 71, 16))
        self.label.setObjectName("label")
        self.textBrowser = QtWidgets.QTextBrowser(self.groupBox)
        self.textBrowser.setGeometry(QtCore.QRect(110, 30, 256, 51))
        self.textBrowser.setObjectName("textBrowser")
        self.lcdNumber = QtWidgets.QLCDNumber(self.groupBox)
        self.lcdNumber.setGeometry(QtCore.QRect(110, 120, 251, 41))
        self.lcdNumber.setObjectName("lcdNumber")
        self.progressBar = QtWidgets.QProgressBar(self.groupBox)
        self.progressBar.setGeometry(QtCore.QRect(30, 230, 341, 23))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")
        self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
        self.calendarWidget.setGeometry(QtCore.QRect(650, 20, 312, 183))
        self.calendarWidget.setObjectName("calendarWidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1125, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.groupBox.setTitle(_translate("MainWindow", "GroupBox"))
        self.label.setText(_translate("MainWindow", "Hello World"))
View Code

 

那么如今怎么把我们画的图运行出来呢,我们写一个first.py来运行它。

first.py

import sys
import firstpic
from PyQt5.QtWidgets import QApplication, QMainWindow

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = firstpic.Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
View Code

 

然后右击运行first.py就可以弹出窗口了,学会了吗。

Guess you like

Origin www.cnblogs.com/smart-zihan/p/12148990.html