前言
Python+PyQt5实现简单计算器开发,界面由PyQt5的栅格布局gridLayout实现。只能计算0-9之间的加减乘除算法,且只能计算一次算法,即存在2个或2个以上的加减乘除不能实现。
效果图:
代码如下
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'calculator.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication,QMainWindow,QPushButton,QWidget
import sys
#定义全局变量
mark = 1 #判断是否点击加减乘除
num1=0 #没有点击加减乘除时把0-9的第一个数字赋值给num1
num2=0 #点击加减乘除后把0-9的第二个数字赋值给num2
result=0 #加减乘除的计算结果放在result
sigal="" #用作等于函数判断是属于加减乘除的哪一个运算
class Ui_widget(object):
def setupUi(self, widget):
widget.setObjectName("widget")
widget.resize(336, 273)
self.gridLayout = QtWidgets.QGridLayout(widget)
self.gridLayout.setObjectName("gridLayout")
self.btn_7 = QtWidgets.QPushButton(widget)
self.btn_7.setObjectName("btn_7")
self.gridLayout.addWidget(self.btn_7, 2, 0, 1, 1) #第2行第0列,占1行1列的大小
self.btn_4 = QtWidgets.QPushButton(widget)
self.btn_4.setObjectName("btn_4")
self.gridLayout.addWidget(self.btn_4, 3, 0, 1, 1)
self.btn_1 = QtWidgets.QPushButton(widget)
self.btn_1.setObjectName("btn_1")
self.gridLayout.addWidget(self.btn_1, 4, 0, 1, 1)
self.btn_0 = QtWidgets.QPushButton(widget)
self.btn_0.setObjectName("btn_0")
self.gridLayout.addWidget(self.btn_0, 5, 0, 1, 1)
self.btn_8 = QtWidgets.QPushButton(widget)
self.btn_8.setObjectName("btn_8")
self.gridLayout.addWidget(self.btn_8, 2, 1, 1, 1)
self.btn_5 = QtWidgets.QPushButton(widget)
self.btn_5.setObjectName("btn_5")
self.gridLayout.addWidget(self.btn_5, 3, 1, 1, 1)
self.btn_2 = QtWidgets.QPushButton(widget)
self.btn_2.setObjectName("btn_2")