This article is participating in Python Theme Month. See the link for details

Hello, I’m Brother Chen ~~~

Objective: To build a Window desktop application: online chat robot.

Today Brother Chen teach you to do an online chat dialogue robot desktop application, has been packaged as an exe executable file, the reader can be directly used, first on the demo diagram

Chat client desktop application

Design Ui: Pyqt5

Answer: Turing robot

Package EXE: Pyinstaller

This article through the interface design, back-end processing, effect demonstration, packaging exe these four aspects to explain.

Pack the EXE download address: wwi.lanzoui.com/iCygNrp0c6f

01. Interface design

1. Install pyqt5

1. Install PyQt5

pip install PyQt5
Copy the code

2. Install the Qt Designer GUI development tool

pip install PyQt5-tools
Copy the code

2. Design the interface

Search in the search box for: Desginer

Creating a Desktop Application

Start the design with controls on the left, design results in the middle, and control property Settings on the right.

The final design result is as follows :(simple design by brother Chen)

Save as the suffix of UI, for example, chenenge. UI

3. The UL interface is converted to Python code

A saved UL file designed by Desginer can be converted directly into Python code with the following command:

pyuic5 -o chenge.py chenge.ui
Copy the code

Preview code

02. Back-end processing

1. Start the page

The chenenge. Py file is not executable, we need to create a new main function to call chenenge

Here is a new py file: main.py

Edit the main py

Import sys import chenge # GUI file name,chenge. UI from Pyqt5. QtWidgets import QApplication, QMainWindow, QWidget if __name__ == '__main__': app = QApplication(sys.argv) MainWindow = QMainWindow() ui = chenge.Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())Copy the code

Running results:

At this time the interface click and input is no response, can only see can not use! Let’s start writing the control processing logic.

2. Plug in the Turing robot

Create a new chat method in the main.py file: Pass in the question and return the solution by calling chat

3. Control processing logic

Bind listening events

1. Input box

2. Display box

3. Button (send)

In the chenge.py file

# binding to monitor events self. PushButton. Clicked. Connect (MainWindow. PushButton) QtCore. QMetaObject. ConnectSlotsByName (MainWindow) MainWindow.setTabOrder(self.history, self.pushButton) MainWindow.setTabOrder(self.textEdit, self.pushButton)Copy the code

Logic:

1. Obtain the input box

2. Call Chat (Turing robot)

3. Add to history

4. Clear the input box

def pushButton(self): Question # q_text = self. UI. TextEdit. ToPlainText self (#) to empty the input box. The UI. TextEdit. The clear () # call Turing robot a_text = self. Chat (q_text a_text =) "" robot" : "+ a_text # # record to chat history = self. UI. History. ToPlainText history = history () +" \ n "+" 【 I 】 : "+q_text+"\n"+a_text self.ui.history.setText(history)Copy the code

03 Effect Demonstration

Dynamic graph:

The final figure:

04, Pack exe

Package the Python files into an exe executable

By command:

pyinstaller -F -w main.py

In our tests, the packaged EXE can be executed on any computer and works well with the host python environment

Because Chen elder brother technology is limited, there is a picture packaging error, so here Chen elder brother first remove the picture, then consider how to pack with pictures

Pack the EXE download address: wwi.lanzoui.com/iCygNrp0c6f

05, subtotal

Objective: To build a Window desktop application: online chat robot. From interface design, to processing back-end conversations, and finally packaged as exe.

Not the small partner, feel hands-on practice !!!! Finally say: original is not easy, ask to praise!