A desktop application
What is a desktop application? Programs that can be run on the visual desktop of an operating system.
For example, QQ, wechat, iQiyi and so on are desktop applications.
What languages were used to develop early desktop applications?
Desktop application software: in the operating system, application content is displayed through the desktop form of the operating system and user interaction is realized. There are many kinds of programming languages, but not all of them have the ability to support UI development. Many languages support UI development ability, and some people implement UI development support platform for it. To put it simply, it is necessary to have special language UI library support, such as Qt library in C++, MFC library, Java Swing, AWT related dependency library, otherwise language is not able to carry out rapid UI development.
Now we can develop in Python, simple and efficient! Can Python develop desktop applications directly?
No! To install Python’s PyQt5 development GUI program. PyQt5 is a C++ based Qt5 library, Qt5 is used for desktop application development, PyQt5 is a C++ implementation of Qt5 package into Python convenient library package.
A quick introduction to PyQt5.
PyQt is a toolkit for creating GUI applications. It is a successful fusion of the Python programming language and the Qt library. The Qt library is one of the most powerful libraries available. PyQt was developed by Phil Thompson.
PyQt is a collection of Python modules with over 300 classes and nearly 6,000 functions and methods. It is a multi-platform toolkit that runs on all major operating systems, including UNIX, Windows and Mac. PyQt is dual-licensed, allowing developers to choose between GPL and commercial licenses. Prior to this, the GPL version was only available on Unix, and starting with PyQt version 4, the GPL license was available on all supported platforms.
PyQt5 can do these desktop applications.
Environmental installation
Conda environment
You are advised to install the Anaconda integrated environment (conDA for short). By default, the anaconda integrated environment (Numpy/Pandas), crawler Scrapy framework, Web framework, and PyQt are installed. Here is the post-installation directory:
drwxr-xr-x 3 apple staff 96 2 25 2019 Anaconda-Navigator.app
drwxr-xr-x 449 apple staff 14368 10 10 18:48 bin
drwxr-xr-x 269 apple staff 8608 2 25 2019 conda-meta
drwxr-xr-x 3 apple staff 96 2 25 2019 doc
drwxr-xr-x 9 apple staff 288 11 26 14:40 envs
drwxr-xr-x 6 apple staff 192 2 25 2019 etc
drwxr-xr-x 305 apple staff 9760 5 17 2019 include
drwxr-xr-x 732 apple staff 23424 2 25 2019 lib
drwxr-xr-x 5 apple staff 160 2 25 2019 libexec
drwxr-xr-x 3 apple staff 96 2 25 2019 man
drwxr-xr-x 68 apple staff 2176 2 25 2019 mkspecs
-rw-rw-r-- 1 apple staff 745 2 25 2019 org.freedesktop.dbus-session.plist
drwxr-xr-x 15 apple staff 480 2 25 2019 phrasebooks
drwxr-xr-x 1086 apple staff 34752 9 29 18:05 pkgs
drwxr-xr-x 25 apple staff 800 2 25 2019 plugins
drwxr-xr-x 3 apple staff 96 2 25 2019 python.app
drwxr-xr-x 27 apple staff 864 2 25 2019 qml
drwxr-xr-x 7 apple staff 224 2 25 2019 resources
drwxr-xr-x 14 apple staff 448 2 25 2019 sbin
drwxr-xr-x 25 apple staff 800 2 25 2019 share
drwxr-xr-x 9 apple staff 288 2 25 2019 ssl
drwxr-xr-x 290 apple staff 9280 2 25 2019 translations
Copy the code
In the bin directory, there is a Designer. App application that is the Designer Designer for PyQt. The extension of the file is.ui.
Since the base environment is installed by default, you can use the Coda command to create a new development environment:
conda create -n gui python=python37.
Copy the code
Activate the environment
conda activate gui
Copy the code
Install pyqt5
(gui) > pip install pyqt5==5.10
Copy the code
If you install PyQt5 later than 5.10, some libraries will be installed separately, such as WebEngine
(gui) > pip install PyQtWebEngine
Copy the code
PyCharm tools
Coding IDE development Tools using PyCharm requires the addition of two Tools extensions.
- UI design tools
Locate the Tools menu item on the left side of the Tool Settings page, select it and click the + button on the right to add a new Tool. Type qtDisigner in the ‘Name’ field and specify the full path to the Program in the Tool Settings below. Such as in Mac environment/Users/apple/anaconda3 / bin/Designer. The app, and input $, FileDir, $in the Working directory.
If Anaconda is installed in the user’s directory on drive C, you need to install Pyqt-tools in CMD and find Designer.exe in the user’s directory.
pip install pyqt5-tools==5.92.1.3. --user
Copy the code
The Program location is C:\Users\{user name}\AppData\Roaming\Python\Python37\ site-Packages \ PyQt5_tools \designer.exe
In addition, the Working directory can also be set to the current project directory.
- Conversion tool
When the UI design is complete, the UI needs to be translated into PY scripts for further coding. Just like adding UI design Tools, add pyuic5 to Tools, Tool Settings Program is pyuic5,Parameters is $FileName$-o $FileNameWithoutExtension$_rc.py,Working Director is $FileDir$.
Combat: PyQt5+Socket to achieve centralized network services
A brief introduction to sockets is required.
What is a Socket? Socket is the intermediate software abstraction layer of communication between application layer and TCP/IP protocol family. It is a group of interfaces. In the design mode, Socket is actually a facade mode, it hides the complex TCP/IP protocol family behind the Socket interface, for the user, a simple set of interfaces is all, let the Socket to organize data to conform to the specified protocol.
The connection process between sockets can be divided into three steps, depending on how the connection is initiated and to which destination the local socket is connected:
(1) Server monitoring.
(2) Client request.
(3) Connection confirmation
The scene in life is:
A scene from life. You want to call a friend, dial first, the friend hears the phone ring and picks up the phone, then you and your friend have established a connection and can talk. When the conversation is over, hang up the phone and end the conversation. Real-life scenarios explain how this works.
So to implement PyQt5+Socket we need to prepare the client and server side:
Server side (partial code) :
if __name__ == '__main__':
datas = DataSource().load()
Create a socket application service
server = socket.socket()
server.bind(('localhost'.18900)) Bind a Host IP address to a Host
server.listen()
print('Central service started \n Waiting for client connection... ')
while True:
client, addr = server.accept()
ClientThread(client, addr).start()
time.sleep(0.5)
Copy the code
Client (partial code) :
class CenterClient() :
def __init__(self, server, port) :
super().__init__()
self.server = server
self.port = port
self.isConnected = False
self.client = None
def connect(self) :
self.client = socket.socket()
self.client.connect((self.server, self.port))
msg = self.client.recv(8*1024)
if msg == b'OK 200':
print('-- Connection successful --')
self.isConnected = True
else:
print('-- Connection failed --')
self.isConnected = False
def send_cmd(self, cmd) :
self.client.send(cmd.encode('utf-8'))
data = self.client.recv(8*1024)
print('{} command result: {}'.format(cmd, data))
if data == b'Error':
return '400'
return data.decode('utf-8')
Copy the code
Effect display:The detailed code and video tutorials for this article have been uploaded to the web disk. The public account “Python Column” repliesA desktop application
Pick it up. If you have any questions, you can contact us to join the free experience class or Q group: 591897914.