Akik Look at that coder

Public account: Look at that code farmer

1. Project introduction

In the previous article, we had a basic introduction to the Qt Designer for PyQt.

In today’s article, we will show you how to design a calculator that calculates integers using Qt Designer.

For people who have used Qt Designer can quickly master PyQt5 and Qt Designer with the use of the method, for beginners to master PyQt5 programming has a great help.

  • Embed Qt Designer | project resume in Pycharm
  • PyQt5’s 13 common modules | Project review
  • PyQt5’s 7 main classes | Project review
  • 7 GUI development tools supported by Python | Project Review
  • Ramble on PyQt- Multi-window setup | Project Recovery

Calculator small program renderings:

2. Configure the project

  • Pycharm
  • Python3.x

3. Development process

1. Open Qt Designer in Pycharm

As follows:

Because the interface is designed without a menu bar, select Widget for the window here. The Widget window created does not have a menu bar.

2. Design the CALCULATOR UI in Qt Designer.

3. Modify the objectName of each control to a unified format.

For example, the objectName of button “1” is as follows

Follow this naming format to change the objectName of the other controls in turn.

4. Turn calculator. UI into calculator.py in Pycharm

5. Create runcal.py

The runcal.py file was created so that each control in calculator. UI would have its own functionality. Let these controls can achieve the corresponding function function, rather than do a decoration. The controls in these Windows implement the signal-and-slot mechanism.

The code for runcal.py is shown below

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from calculator import Ui_Form
import os,sys

global e_view


class MyMainWindow(Ui_Form.QWidget) :def forge_link(self) :self.b_0.clicked.connect(self.button_event(0))
        self.b_1.clicked.connect(self.button_event(1))
        self.b_2.clicked.connect(self.button_event(2))
        self.b_3.clicked.connect(self.button_event(3))
        self.b_4.clicked.connect(self.button_event(4))
        self.b_5.clicked.connect(self.button_event(5))
        self.b_6.clicked.connect(self.button_event(6))
        self.b_7.clicked.connect(self.button_event(7))
        self.b_8.clicked.connect(self.button_event(8))
        self.b_9.clicked.connect(self.button_event(9))
        self.b_add.clicked.connect(self.button_event(' + '))self.b_sub.clicked.connect(self.button_event(' - '))self.b_mul.clicked.connect(self.button_event(' * '))self.b_div.clicked.connect(self.button_event('/'))
        self.b_pow.clicked.connect(self.button_event(' * * '))self.b_bra_l.clicked.connect(self.button_event(' ('))self.b_bra_r.clicked.connect(self.button_event(')))self.b_mod.clicked.connect(self.button_event(' % '))self.b_pai.clicked.connect(self.button_event(' 3.1415926 '))self.b_pt.clicked.connect(self.button_event('. '))self.b_del.clicked.connect(self.delete_event)
        self.b_clc.clicked.connect(self.clear_event)
        self.b_eq.clicked.connect(self.calc_complish)

    def __init__(self.parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.setWindowTitle('calculator'Self. Forge_link () # def button_event(self,arg): # print(dir(self.e_view))globalE_view e_view=self.e_view def fun(): # return a custom slot functionglobal e_view
            txt = e_view.toPlainText()
            e_view.setText(txt + str(arg))
        return fun

    def calc_complish(self):
        txt=self.e_view.toPlainText()
        ans=' '
        try:
            ans=str(eval(txt))
        except BaseException:
            ans='MathError'
        # print(ans)
        self.clear_event()
        self.e_view.setText(ans)
        self.l_hist.addItem(txt+'='+ans)

    def clear_event(self):
        self.e_view.setText(' ')

    def delete_event(self):
        txt = self.e_view.toPlainText()
        txt=txt[:len(txt)-1]
        self.e_view.setText(txt)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    myWin=MyMainWindow()
    myWin.show()
    sys.exit(app.exec())
Copy the code

For example, the following code is to realize the interface signal and slot mechanism, that is, each button signal on the interface is matched with the corresponding slot function.

        self.b_0.clicked.connect(self.button_event(0))
        self.b_1.clicked.connect(self.button_event(1))
        self.b_2.clicked.connect(self.button_event(2))
        self.b_3.clicked.connect(self.button_event(3))
        self.b_4.clicked.connect(self.button_event(4))
        self.b_5.clicked.connect(self.button_event(5))
        self.b_6.clicked.connect(self.button_event(6))
        self.b_7.clicked.connect(self.button_event(7))
        self.b_8.clicked.connect(self.button_event(8))
        self.b_9.clicked.connect(self.button_event(9))
        self.b_add.clicked.connect(self.button_event('+'))
        self.b_sub.clicked.connect(self.button_event(The '-'))
        self.b_mul.clicked.connect(self.button_event(The '*'))
        self.b_div.clicked.connect(self.button_event('/'))
        self.b_pow.clicked.connect(self.button_event('* *'))
        self.b_bra_l.clicked.connect(self.button_event('('))
        self.b_bra_r.clicked.connect(self.button_event(') '))
        self.b_mod.clicked.connect(self.button_event(The '%'))
        self.b_pai.clicked.connect(self.button_event('3.1415926'))
        self.b_pt.clicked.connect(self.button_event('. '))
        self.b_del.clicked.connect(self.delete_event)
        self.b_clc.clicked.connect(self.clear_event)
        self.b_eq.clicked.connect(self.calc_complish)
Copy the code

The core part of this program is the following slot function body part, when each button signal is sent. The system responds to the signal. If the “1” button is pressed, the system will display “1” on the interface. If the “+” button is pressed, the system will add the signal.

def button_event(self,arg):
        globalE_view e_view=self.e_view def fun(): # return a custom slot functionglobal e_view
            txt = e_view.toPlainText()
            e_view.setText(txt + str(arg))
        return fun
Copy the code

6. Run runcal.py

The results are as follows

4. Summary of project thinking

Design a calculator for the initial learning PyQt5 people have great benefits, which involves the use of various functions, interface design, the content is more detailed. Can learn the basic knowledge from the theory to the practice, and integrate. It is very helpful for beginners to master PyQt5 programming as soon as possible.

If you find this helpful:

1. Click “like” to support it, so that more people can see this article

2, pay attention to the public number: look at that code farmers, we study together and progress together.

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign