【PyQt5-Qt Designer】简易的数字键盘输入+简易计算器
参考如下键盘格式写了一个键盘输入,目前还不能进行运算,后期完善。。。
效果如下:
完整代码:
from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QLCDNumber,QFormLayout,QLineEdit,QLabel,QGridLayout) from PyQt5.QtGui import QFont from PyQt5.QtCore import Qt,QTimer,QDateTime,QDate,QTime,QSize import sys class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.setGeometry(300,300,330,310) self.setWindowTitle("数字键盘输入") gridLayout = QGridLayout() self.display = QLineEdit("0") self.display.setFixedSize(QSize(287,40)) self.display.setReadOnly(True) self.display.setAlignment(Qt.AlignRight) self.display.setFont(QFont("微软雅黑",14,QFont.Bold)) gridLayout.addWidget(self.display,0,0,1,4) self.showNum = "" keys = ['Clear', '/', '*', '-', '7', '8', '9', '+', '4', '5', '6', '', '1', '2', '3', 'Enter', '0', '', '.', ''] position = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 2), (3, 0), (3, 1), (3, 2), (3, 3), (4, 0), (4, 1), (4, 2), (4, 3), ] for item in range(len(keys)): btn = QPushButton(keys[item]) btn.setFixedSize(QSize(60, 40)) btn.setFont(QFont("微软雅黑",12,QFont.Bold)) btn.clicked.connect(self.btnClicked) if keys[item] == "+": gridLayout.addWidget(btn, 2, 3, 2, 1) btn.setFixedSize(QSize(60, 90)) elif keys[item] == "Enter": gridLayout.addWidget(btn, 4, 3, 2, 1) btn.setFixedSize(QSize(60, 90)) elif keys[item] == "0": gridLayout.addWidget(btn, 5, 0, 1, 2) btn.setFixedSize(QSize(142, 40)) elif keys[item] == "": continue else: gridLayout.addWidget(btn, position[item][0]+1, position[item][1], 1, 1) self.setLayout(gridLayout) def btnClicked(self): sender = self.sender() symbols = ["Clear","/","*","-","+","Enter"] if sender.text() not in symbols: self.showNum += sender.text() self.display.setText(self.showNum) elif sender.text() == "Clear": self.display.setText("0") self.showNum = "" if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
今天完善了一下,可以进行简单的+ - * / 运算了,效果如下:
完整代码:
1 from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QLCDNumber,QFormLayout,QLineEdit,QLabel,QGridLayout) 2 from PyQt5.QtGui import QFont 3 from PyQt5.QtCore import Qt,QTimer,QDateTime,QDate,QTime,QSize 4 import sys 5 6 class Example(QWidget): 7 def __init__(self): 8 super(Example, self).__init__() 9 self.initUI() 10 11 def initUI(self): 12 self.setGeometry(300,300,330,310) 13 self.setWindowTitle("简易计算器") 14 gridLayout = QGridLayout() 15 self.display = QLineEdit("0") 16 self.display.setFixedSize(QSize(287,40)) 17 self.display.setReadOnly(True) 18 self.display.setAlignment(Qt.AlignRight) 19 self.display.setFont(QFont("微软雅黑",14,QFont.Bold)) 20 gridLayout.addWidget(self.display,0,0,1,4) 21 self.showNum = "" 22 self.firstNum = "" 23 self.lastNum = "" 24 25 keys = ['Clear', '/', '*', '-', 26 '7', '8', '9', '+', 27 '4', '5', '6', '', 28 '1', '2', '3', 'Enter', 29 '0', '', '.', ''] 30 position = [(0, 0), (0, 1), (0, 2), (0, 3), 31 (1, 0), (1, 1), (1, 2), (1, 3), 32 (2, 0), (2, 1), (2, 2), (2, 2), 33 (3, 0), (3, 1), (3, 2), (3, 3), 34 (4, 0), (4, 1), (4, 2), (4, 3), ] 35 for item in range(len(keys)): 36 btn = QPushButton(keys[item]) 37 btn.setFixedSize(QSize(60, 40)) 38 btn.setFont(QFont("微软雅黑",12,QFont.Bold)) 39 btn.clicked.connect(self.btnClicked) 40 if keys[item] == "+": 41 gridLayout.addWidget(btn, 2, 3, 2, 1) 42 btn.setFixedSize(QSize(60, 90)) 43 elif keys[item] == "Enter": 44 gridLayout.addWidget(btn, 4, 3, 2, 1) 45 btn.setFixedSize(QSize(60, 90)) 46 elif keys[item] == "0": 47 gridLayout.addWidget(btn, 5, 0, 1, 2) 48 btn.setFixedSize(QSize(142, 40)) 49 elif keys[item] == "": 50 continue 51 else: 52 gridLayout.addWidget(btn, position[item][0]+1, position[item][1], 1, 1) 53 self.setLayout(gridLayout) 54 55 def btnClicked(self): 56 sender = self.sender() 57 symbols = ["Clear","/","*","-","+","Enter"] 58 if sender.text() not in symbols: 59 self.showNum += sender.text() 60 self.display.setText(self.showNum) 61 elif sender.text() == "Clear": 62 self.display.setText("0") 63 self.showNum = "" 64 elif sender.text() == "/": 65 self.firstNum = self.showNum 66 self.display.setText("0") 67 self.showNum = "" 68 self.operator = "/" 69 elif sender.text() == "*": 70 self.firstNum = self.showNum 71 self.display.setText("0") 72 self.showNum = "" 73 self.operator = "*" 74 elif sender.text() == "-": 75 self.firstNum = self.showNum 76 self.display.setText("0") 77 self.showNum = "" 78 self.operator = "-" 79 elif sender.text() == "+": 80 self.firstNum = self.showNum 81 self.display.setText("0") 82 self.showNum = "" 83 self.operator = "+" 84 85 elif sender.text() == "Enter": 86 if self.operator == "/": 87 result = float(self.firstNum) / float(self.showNum) 88 result = float("%.2f"%result) 89 elif self.operator == "*": 90 result = float(self.firstNum) * float(self.showNum) 91 result = float("%.2f" % result) 92 elif self.operator == "+": 93 result = float(self.firstNum) + float(self.showNum) 94 result = float("%.2f" % result) 95 elif self.operator == "-": 96 result = float(self.firstNum) - float(self.showNum) 97 result = float("%.2f" % result) 98 self.display.setText(str(result)) 99 self.showNum = "" 100 101 102 if __name__ == '__main__': 103 app = QApplication(sys.argv) 104 ex = Example() 105 ex.show() 106 sys.exit(app.exec_())