PyQt5 entry

 PyQt5 is used to create Python GUI applications toolkit. As a cross-platform toolkit, PyQt runs (Unix, Windows, Mac) on all major operating systems.

Under article describes how to install Windows system Python + PyCharm + PyQt5, and in two ways GUI interface design through PyQt5 used. . A direct use of code design interface;. B QtDesigner visualized using the first design, and then convert the resulting file into .ui .py file.

 

Installation Python + PyCharm + PyQt5

1, install Python

Visit the official website https://www.python.org/, download and install your goal Python version.

 

2, installation PyQt5

1) enter cmd interface. Run pip install pyqt5 pyqt5-tools, wait for a while, after the implementation of PyQt5 command is now installed.

During the installation process may report the following error, you can not find pyqt5-tools version corresponding.

  Collecting pyqt5-tools
  Could not find a version that satisfies the requirement pyqt5-tools (from versions: )
No matching distribution found for pyqt5-tools

Since the original author PC 2016 installed a version of python3.5, fortunately, the first execution pip install pyqt5 pyqt5-tools is completed, all the installation finished. But later updated to have encountered this problem python3.5.4 and python3.7 time. python3.5.4 PIP after updating to the latest version, the problem seems to have disappeared. python3.7 did not go to solve.

2) test the python IDLE comes in to see if pqyt5 really fitted.

New File and enter the following code firstPython.py

#####################################

import sys  
  
from PyQt5 import QtWidgets, QtCore  
  
app = QtWidgets.QApplication(sys.argv)  
widget = QtWidgets.QWidget()  
widget.resize(400, 100)  
widget.setWindowTitle("This is a demo for PyQt Widget.")  
widget.show()  
  
exit(app.exec_()) 

#####################################

After running the following interface pops up, it represents PyQt has worked properly.

 

If, unfortunately, there is no GUI window will pop up and prompt an error: ImportError: DLL load failed: The specified module could not be found.

It is said that this is due to the lack of suitable python3.dll, you can go through the required version of python python.org download and install the installation package, and then copy the files from the installation directory python3.dll, and python3x.dll own use in the same to the next level directory.

The author's own original installation python3.5 is hanging in this step, then reinstall deleted directly python3.5.4, they encountered above 1) the problem. Inexplicable pass after the upgrade version pip. We suggest that you follow the recommended way to solve the above operations.

3, installation PyCharm

1) Access the official website http://www.jetbrains.com/pycharm/download/#section=windows authors choose to download and install Community Edition - currently PyCharm 2018.2.2 Community Edition.

 

2) PyCharm Basic configuration:

. A the establishment of a new project: first. 

b. Set the default PyCharm parser

Select File | Settings | Project: first | Project Interpreter, set the python version you use Project Interpreter

For example, C: \ xxxx \ AppData \ Local \ Programs \ Python \ Python35-32 \ python.exe

c. Add third-party libraries 

 Stay in Project Interpreter interface, click +, find and install pyqt5, pyqt5-sip, pyqt5-tools. After successful installation return, the interface should be as follows.

 

d. Configure PyQt

PyCharm select File | Settings | Tools | External Tools, click + New tools to build QTdesigner tools and PyUIC

QtDesigner:

 

Configuring two key parameters:

Program: designer.exe own path such as C: \ xxxx \ AppData \ Local \ Programs \ Python \ Python35-32 \ Lib \ site-packages \ pyqt5-tools \ designer.exe  

Working directory:$ProjectFileDir$

 

PyUIC:

 

Configuration three key parameters:

Program: python.exe own path such as C: \ xxxx \ AppData \ Local \ Programs \ Python \ Python35-32 \ python.exe

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

Working directory:$ProjectFileDir$

 

ExternalTools interface tool returns after successful establishment, as follows:

 

Return to the main screen, Tools-> ExternalTools added a QTdesigner and PyUIC two tools

 

4, PyCharm GUI interface design through PyQt5

1) use the code to create a GUI interface

New firstGUI.py, enter the following code

# - * - Coding: UTF-8 - * -
"" "the first program." ""

from PyQt5 import QtWidgets # introduction member PyQt5

import sys

app = QtWidgets.QApplication (sys.argv) # establish application objects

first_window = QtWidgets.QWidget () # establish a form object

first_window.resize (400, 300) # Set form size

first_window.setWindowTitle ( "My first pyqt program") # Set the form title

first_window.show () # display the form

sys.exit (app.exec ()) # run the program
##################################### ##########################

Select Run-> Run firstGUI, the results show the following interface will be successful.

 

 

2) using QT Designer code generates a GUI interface, and is converted to .py file PyUIC.

Select Tools-> ExternalTools-> QTdesigner, enter QT Designer interface, first create a GUI interface will pop up, click on creat automatically generated with a button below the GUI interface, as shown below.

 

Save the file interface Utitled.ui.

Return PyCharm project interface, the list of items under more out of this .ui file

Right-click Utitled.ui, the pop-up list, select ExternalTools-> PyUIC will generate Utitled.ui corresponding Utitled.py files. Follows

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")

self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
#############################################################################
That's All .Thankyou~

 

Guess you like

Origin www.cnblogs.com/daofaziran/p/11035395.html