from PyQt5 import QtWidgets, uic, QtGui import os url = os.path.dirname(os.path.abspath(__file__))+os.sep+'cal.ui' class MyWindow(QtWidgets.QWidget): def __init__(self, parent=None): global d, a QtWidgets.QWidget.__init__(self, parent) uic.loadUi(url , self) a = 0 d = '' def addnumb(text): if text == '.': if self.label.text().count('.') < 1: self.label.setText(str(self.label.text()+text)) else: if self.label.text() == '0': self.label.setText(str(int(self.label.text()+text))) else: self.label.setText(str(self.label.text()+text)) def delete(): if len(self.label.text())>1: self.label.setText(self.label.text()[:len(self.label.text())-1]) else: self.label.setText('0') def clear(): self.label.setText('0') a = 0 def summ(): global d, a if self.label.text().count('.')==0: a = int(self.label.text()) else: a = float(self.label.text()) d = '+' self.label.setText('0') def difference(): global d, a if self.label.text().count('.')==0: a = int(self.label.text()) else: a = float(self.label.text()) d = '-' self.label.setText('0') def product(): global d, a if self.label.text().count('.')==0: a = int(self.label.text()) else: a = float(self.label.text()) d = '*' self.label.setText('0') def div(): global d, a if self.label.text().count('.')==0: a = int(self.label.text()) else: a = float(self.label.text()) d = '/' self.label.setText('0') def work(a, b): global d if b.count('.')==0: b = int(b) else: b = float(b) if d != '': if d == '+': self.label.setText(str(a+b)) elif d == '-': self.label.setText(str(a-b)) elif d == '*': self.label.setText(str(a*b)) elif d == '/': self.label.setText(str(a/b)) a = 0 d = '' def pr(): global a b = str((a / 100) * float(self.label.text())) work(a, b) def pm(): if float(self.label.text())>0: self.label.setText('-'+self.label.text()) else: self.label.setText(self.label.text()[1:]) self.but_1.clicked.connect(lambda:addnumb('1')) self.but_2.clicked.connect(lambda:addnumb('2')) self.but_3.clicked.connect(lambda:addnumb('3')) self.but_4.clicked.connect(lambda:addnumb('4')) self.but_5.clicked.connect(lambda:addnumb('5')) self.but_6.clicked.connect(lambda:addnumb('6')) self.but_7.clicked.connect(lambda:addnumb('7')) self.but_8.clicked.connect(lambda:addnumb('8')) self.but_9.clicked.connect(lambda:addnumb('9')) self.but_0.clicked.connect(lambda:addnumb('0')) self.but_dot.clicked.connect(lambda:addnumb('.')) self.but_del.clicked.connect(delete) self.but_c.clicked.connect(clear) self.but_pm.clicked.connect(pm) self.but_plus.clicked.connect(summ) self.but_minus.clicked.connect(difference) self.but_mnoz.clicked.connect(product) self.but_div.clicked.connect(div) self.but.clicked.connect(lambda:work(a, self.label.text())) self.but_pr.clicked.connect(pr) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_())