部分代码:
elif name=="确认":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 120))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 3, 3,2,1)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
完整代码:
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtGui import QFont
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Calculator(QDialog):
Signal= pyqtSignal(str)
def __init__(self, parent = None):
super(Calculator, self).__init__(parent)
self.initUI()
self.reset()
self.display.setText("0")
def initUI(self):
self.setGeometry(300,300,330,310)
self.setWindowTitle('数字密码开门')
Grid = QtWidgets.QGridLayout()
self.display = QtWidgets.QLineEdit('')
self.display.setFont(QFont("微软雅黑",14,QFont.Bold))
self.display.setReadOnly(True)
self.display.setAlignment(QtCore.Qt.AlignRight)
self.display.setMaxLength(20)
Grid.addWidget(self.display, 0, 0, 1, 4)
names = [
'1', '2', '3', '退出',
'4', '5', '6', '删除',
'7', '8', '9', '确认',
'清零', '0', '.', '']
pos = [(0, 0), (0, 1), (0, 2), (0, 3),
(1, 0), (1, 1), (1, 2), (1, 3),
(2, 0), (2, 1), (2, 2), (2, 3),
(3, 0), (3, 1), (3, 2), (3, 3),
(4, 0), (4, 1), (4, 2), (4, 3)]
c = 0
# for name in names:
# button = QtWidgets.QPushButton(name)
# button.setFixedSize(QtCore.QSize(100, 60))
# button.setFont(QFont("微软雅黑",14,QFont.Bold))
# button.clicked.connect(self.ButtonClicked)
#
# Grid.setSpacing(5)
# if name=="":
# continue
# elif name=="确认":
# Grid.addWidget(button, 3, 3,2,1)
# button.setFixedSize(QSize(100, 120))
# button.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
# Grid.addWidget(button, pos[c][0] + 1, pos[c][1],1,1)
# c = c + 1
for name in names:
if name=="1":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑",14,QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 1, 0)
elif name=="2":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 1, 1)
elif name=="3":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 1, 2)
elif name=="退出":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 1, 3)
elif name=="4":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 2, 0)
elif name=="5":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 2, 1)
elif name=="6":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 2, 2)
elif name=="删除":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 2, 3)
elif name=="7":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 3, 0)
elif name=="8":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 3, 1)
elif name=="9":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 3, 2)
elif name=="确认":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 120))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 3, 3,2,1)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
elif name=="清零":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 4, 0)
elif name=="0":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 4, 1)
elif name==".":
button = QtWidgets.QPushButton(name)
button.setFixedSize(QtCore.QSize(100, 60))
button.setFont(QFont("微软雅黑", 14, QFont.Bold))
button.clicked.connect(self.ButtonClicked)
Grid.addWidget(button, 4, 2)
elif name=="":
continue
self.setLayout(Grid)
def ButtonClicked(self):
text = self.sender().text()
if text == "确认":
r = eval(self.number)
result_1 = str(r).encode('utf-8')
result=result_1.decode('utf-8')
self.display.setText(str(result)) #计算结果
self.number = result
# 按钮链接
content = result
self.Signal.emit(content)
elif text == "删除":
self.number = self.number[:-1]
self.display.setText(self.number)
elif text == "清零":
self.reset()
self.display.setText(self.number)
elif text=="退出":
self.close()
else:
if text in "+-*/":
self.number = self.number + text
else:
self.number = self.number + text if self.number != "0" else text
self.display.setText(self.number)
def reset(self):
self.number = "0"
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Calculator()
ex.show()
sys.exit(app.exec_())
原贴:按钮占两行https://blog.csdn.net/mangobar/article/details/73775381
计算器:https://www.cnblogs.com/juan-F/archive/2018/08/10/9455512.html