This case is from chapter 7 of “MATLAB Object-oriented Programming — From Getting Started to Design Mode (2nd edition)”. For details on the design of GUI tools for deposit and withdrawal, please refer to pages 127~160.

The final interface created with PyQt5 is as follows:

Let’s talk about the implementation details.

Compared with the Matlab design version, the only difference lies in the realization of the event processing mechanism is different.

PyQt5/Qt, called signals and slots, is used for communication between objects. When a specified event occurs, an event signal is emitted and slots can be called by any Python script. The slot is called when a signal connected to the slot is emitted. Custom events are done by the pyqtSignal method in PyQt5.qtCore.

The Handle class in Matlab has also helped us to realize the event processing mechanism. In addition to the built-in properties of the control, such as Callback and ButtonPushedFcn (which can also be understood as built-in events), we can also customize the events definition. Events are then registered via notify (corresponding to signal emission in Qt!). Finally, addListener listens for the event (which acts as a slot).

The source code of the model class is as follows:

 # !/usr/bin/env python3
 # -*- coding:utf-8 -*-
 ​
 from PyQt5.QtCore import QObject, pyqtSignal
 ​
 ​
 class BalanceModel(QObject) :
 ​
     balance_changed = pyqtSignal()
 ​
     def __init__(self, balance) :
         super(BalanceModel, self).__init__()
         self.balance = balance
 ​
     def withdraw(self, val) :
         self.balance -= val
         self.balance_changed.emit()
 ​
     def deposit(self, val) :
         self.balance += val
         self.balance_changed.emit()
 ​
Copy the code

An event signal is customized by the statement balance_changed = pyqtSignal() : balance_changed. In the model class, when a withdrawal or deposit occurs and the balance changes, the Balance_CHANGED event signal is fired and a message is published (emitted) (by binding the emit() method). Next, you need to define a so-called slot function in the view class that listens for the Balance_CHANGED event signal and responds.

The view class source code is as follows:

 # !/usr/bin/env python3
 # -*- coding:utf-8 -*-
 ​
 from PyQt5.QtWidgets import (QWidget,
                              QGridLayout,
                              QLabel,
                              QLineEdit,
                              QPushButton)
 from PyQt5.QtCore import Qt
 from BalanceController import BalanceController
 ​
 ​
 class BalanceView(QWidget) :
 ​
     def __init__(self, m_obj) :
         super(BalanceView, self).__init__()
         self.m_obj = m_obj
         self.c_obj = self.make_controller()  # Controller object
         self.balance_label = None
         self.balance_text = None
         self.rmb_label = None
         self.rmb_text = None
         self.withdraw_btn = None
         self.deposit_btn = None
         self.build_app()
         self.attach2controller(self.c_obj)
         self.m_obj.balance_changed.connect(self.update_balance)
 ​
     def build_app(self) :
         self.setVisible(False)
         self.resize(300.120)
         self.setWindowTitle('Deposit and Withdrawal Interface')
 ​
         self.balance_label = QLabel('Balance')
         self.balance_label.setAlignment(Qt.AlignRight)
         self.balance_text = QLineEdit()
         self.balance_text.setAlignment(Qt.AlignRight)
         self.balance_text.setReadOnly(True)
         self.balance_text.setText('0')
         self.rmb_label = QLabel('RMB')
         self.rmb_label.setAlignment(Qt.AlignRight)
         self.rmb_text = QLineEdit()
         self.rmb_text.setAlignment(Qt.AlignRight)
         self.rmb_text.setText('0')
         self.withdraw_btn = QPushButton('withdraw')
         self.withdraw_btn.setAutoFillBackground(True)
         self.deposit_btn = QPushButton('deposit')
 ​
         main_layout = QGridLayout(self)
         main_layout.setHorizontalSpacing(15)
         main_layout.setVerticalSpacing(15)
 ​
         main_layout.addWidget(self.balance_label, 0.0.1.2)
         main_layout.addWidget(self.balance_text, 0.2.1.2)
         main_layout.addWidget(self.rmb_label, 1.0.1.2)
         main_layout.addWidget(self.rmb_text, 1.2.1.2)
         main_layout.addWidget(self.withdraw_btn, 2.0.1.2)
         main_layout.addWidget(self.deposit_btn, 2.2.1.2)
 ​
         self.setLayout(main_layout)
         self.setVisible(True)
         self.update_balance()
 ​
     def update_balance(self) :
         self.balance_text.setText(str(self.m_obj.balance))
 ​
     def make_controller(self) :
         controller = BalanceController(self, self.m_obj)
         return controller
 ​
     def attach2controller(self, controller) :
         self.withdraw_btn.clicked.connect(controller.withdraw_btn_callback)
         self.deposit_btn.clicked.connect(controller.deposit_btn_callback)
 ​
Copy the code

For button controls, you need a connect method to listen for the event signal: self.m_obj.balance_change.connect (self.update_balance)

Controller class source code is as follows:

 # !/usr/bin/env python3
 # -*- coding:utf-8 -*-
 ​
 ​
 class BalanceController:
 ​
     def __init__(self, v_obj, m_obj) :
         self.v_obj = v_obj
         self.m_obj = m_obj
 ​
     def withdraw_btn_callback(self) :
         val = float(self.v_obj.rmb_text.displayText())
         self.m_obj.withdraw(val)
 ​
     def deposit_btn_callback(self) :
         val = float(self.v_obj.rmb_text.displayText())
         self.m_obj.deposit(val)
 ​
Copy the code

Finally, we can customize the balanceapp.py script to put together the MVC source code to run the GUI widget, as shown in the following GIF:

At this point, we have introduced a relatively complete use of MVC design pattern to build GUI method! I hope you enjoy it and get something useful from it.

This article complete code, please return “pyQt5_mvc” in GZH for download.

【 Previous recommendation 】

  • Learn how to use Matlab App Designer to design text recognition tools.
  • Vectorization Method for Matrix 2-Normalizing (QQ.com)
  • TexStudio Theme Configuration (QQ.com)
  • Giving benefits (QQ.com)
  • How to use Matlab to download STATION B HD video with one click (part 2)
  • Decorators in Python (qq.com)
  • MATLAB Style Guide 2.0 (QQ.com)
  • (qq.com)
  • Guess what today’s dry goods are? (qq.com)
  • Share to climb Matlab Chinese forum basic discussion source code (qq.com)
  • How to use Matlab to download HIGH-DEFINITION video of STATION B (PART 1)
  • Crawl the literature on a scholar’s homepage (QQ.com)