初学python,写个计算器作为练手项目。参考了网上很多朋友的代码,但觉得我写的是最为简单的,代码最为精简。
#!/usr/bin/env python
import math
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QGridLayout, QLayout,
QLineEdit, QSizePolicy, QPushButton, QWidget)
class Calculator(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Calculator')
self.resize(300,300)
self.initUI()
self.cline = ""
def initUI(self):
self.numLineEdit = QLineEdit() #用QLineEdit定义一个输入框
self.zeroButton = QPushButton('0') #QPushButton定义各按键
self.zeroButton.clicked.connect(self.zero) #调用按键的槽函数
self.oneButton = QPushButton('1')
self.oneButton.clicked.connect(self.one)
self.twoButton = QPushButton('2')
self.twoButton.clicked.connect(self.two)
self.threeButton = QPushButton('3')
self.threeButton.clicked.connect(self.three)
self.fourButton = QPushButton('4')
self.fourButton.clicked.connect(self.four)
self.fiveButton = QPushButton('5')
self.fiveButton.clicked.connect(self.five)
self.sixButton = QPushButton('6')
self.sixButton.clicked.connect(self.six)
self.sevenButton = QPushButton('7')
self.sevenButton.clicked.connect(self.seven)
self.eightButton = QPushButton('8')
self.eightButton.clicked.connect(self.eight)
self.nineButton = QPushButton('9')
self.nineButton.clicked.connect(self.nine)
self.deleteButton = QPushButton('delete')
self.deleteButton.clicked.connect(self.my_delete)
self.CButton = QPushButton('C')
self.CButton.clicked.connect(self.C)
self.plusButton = QPushButton('+')
self.plusButton.clicked.connect(self.plus)
self.minusButton = QPushButton('-')
self.minusButton.clicked.connect(self.minus)
self.mutiplyButton = QPushButton('*')
self.mutiplyButton.clicked.connect(self.mutiply)
self.divideButton = QPushButton('/')
self.divideButton.clicked.connect(self.divide)
self.equalButton = QPushButton('=')
self.equalButton.clicked.connect(self.equal)
self.dotButton = QPushButton('.')
self.dotButton.clicked.connect(self.dot)
self.leftButton = QPushButton('(')
self.leftButton.clicked.connect(self.left)
self.rightButton = QPushButton(')')
self.rightButton.clicked.connect(self.right)
self.mainLayout = QGridLayout() #用QGridLayout设置计算器的布局和各按键的位置
self.mainLayout.addWidget(self.numLineEdit, 0, 0, 1, 4)
self.mainLayout.addWidget(self.oneButton, 1, 0)
self.mainLayout.addWidget(self.twoButton, 1, 1)
self.mainLayout.addWidget(self.threeButton, 1, 2)
self.mainLayout.addWidget(self.plusButton, 4, 1)
self.mainLayout.addWidget(self.fourButton, 2, 0)
self.mainLayout.addWidget(self.fiveButton, 2, 1)
self.mainLayout.addWidget(self.sixButton, 2, 2)
self.mainLayout.addWidget(self.minusButton, 4, 2)
self.mainLayout.addWidget(self.sevenButton, 3, 0)
self.mainLayout.addWidget(self.eightButton, 3, 1)
self.mainLayout.addWidget(self.nineButton, 3, 2)
self.mainLayout.addWidget(self.mutiplyButton, 4, 0)
self.mainLayout.addWidget(self.zeroButton, 3, 3)
self.mainLayout.addWidget(self.CButton, 1, 3)
self.mainLayout.addWidget(self.deleteButton, 2, 3)
self.mainLayout.addWidget(self.divideButton, 4, 3)
self.mainLayout.addWidget(self.equalButton, 5, 3)
self.mainLayout.addWidget(self.leftButton, 5, 0)
self.mainLayout.addWidget(self.rightButton, 5, 1)
self.mainLayout.addWidget(self.dotButton, 5, 2)
self.setLayout(self.mainLayout)
def C(self): #下面的都是槽函数,定义各个按键的功能
self.cline = ''
self.numLineEdit.setText(self.cline)
def zero(self):
self.cline = self.numLineEdit.text()
if self.cline != '0':
self.cline += "0"
self.numLineEdit.setText(self.cline)
def one(self):
self.cline = self.numLineEdit.text()
self.cline += '1'
self.numLineEdit.setText(self.cline)
def two(self):
self.cline = self.numLineEdit.text()
self.cline += '2'
self.numLineEdit.setText(self.cline)
def three(self):
self.cline = self.numLineEdit.text()
self.cline += '3'
self.numLineEdit.setText(self.cline)
def four(self):
self.cline = self.numLineEdit.text()
self.cline += '4'
self.numLineEdit.setText(self.cline)
def five(self):
self.cline = self.numLineEdit.text()
self.cline += '5'
self.numLineEdit.setText(self.cline)
def six(self):
self.cline = self.numLineEdit.text()
self.cline += '6'
self.numLineEdit.setText(self.cline)
def seven(self):
self.cline = self.numLineEdit.text()
self.cline += '7'
self.numLineEdit.setText(self.cline)
def eight(self):
self.cline = self.numLineEdit.text()
self.cline += '8'
self.numLineEdit.setText(self.cline)
def nine(self):
self.cline = self.numLineEdit.text()
self.cline += '9'
self.numLineEdit.setText(self.cline)
def dot(self):
self.cline = self.numLineEdit.text()
self.cline += '.'
self.numLineEdit.setText(self.cline)
def plus(self):
self.cline = self.numLineEdit.text()
self.cline += '+'
self.numLineEdit.setText(self.cline)
def minus(self):
self.cline = self.numLineEdit.text()
self.cline += '-'
self.numLineEdit.setText(self.cline)
def left(self):
self.cline = self.numLineEdit.text()
self.cline += '('
self.numLineEdit.setText(self.cline)
def right(self):
self.cline = self.numLineEdit.text()
self.cline += ')'
self.numLineEdit.setText(self.cline)
def mutiply(self):
self.cline = self.numLineEdit.text()
self.cline += '*'
self.numLineEdit.setText(self.cline)
def divide(self):
self.cline = self.numLineEdit.text()
self.cline += '/'
self.numLineEdit.setText(self.cline)
def my_delete(self):
self.cline = self.numLineEdit.text()
l = len(self.cline)
self.cline = self.cline[0:l-1]
self.numLineEdit.setText(self.cline)
def equal(self): #最终的计算用的是python自带的计算函数eval
self.cline = self.numLineEdit.text()
self.cline = str(eval(self.cline))
self.numLineEdit.setText(self.cline)
if __name__ == '__main__':
app = QApplication(sys.argv)
cal = Calculator()
cal.show()
app.exec_()