## Ex 5-1. QPushButton.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QGridLayout
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.n1 = int(0)
self.n2 = int(0)
self.a = int(0)
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
btn1 = QPushButton(self)
btn1.setText('1')
btn1.clicked.connect(self.clickBtn1)
grid.addWidget(btn1, 2, 0)
btn2 = QPushButton(self)#2
btn2.setText('2')
btn2.clicked.connect(self.clickBtn2)
grid.addWidget(btn2, 2, 1)
btn3 = QPushButton(self)#3
btn3.setText('3')
btn3.clicked.connect(self.clickBtn3)
grid.addWidget(btn3, 2, 2)
btn4 = QPushButton(self)#4
btn4.setText('4')
btn4.clicked.connect(self.clickBtn4)
grid.addWidget(btn4, 1, 0)
btn5 = QPushButton(self)#5
btn5.setText('5')
btn5.clicked.connect(self.clickBtn5)
grid.addWidget(btn5, 1, 1)
btn6 = QPushButton(self)#6
btn6.setText('6')
btn6.clicked.connect(self.clickBtn6)
grid.addWidget(btn6, 1, 2)
btn7 = QPushButton(self)#7
btn7.setText('7')
btn7.clicked.connect(self.clickBtn7)
grid.addWidget(btn7, 0, 0)
btn8 = QPushButton(self)#8
btn8.setText('8')
btn8.clicked.connect(self.clickBtn8)
grid.addWidget(btn8, 0, 1)
btn9 = QPushButton(self)#9
btn9.setText('9')
btn9.clicked.connect(self.clickBtn9)
grid.addWidget(btn9, 0, 2)
btn0 = QPushButton(self)#0
btn0.setText('0')
btn0.clicked.connect(self.clickBtn0)
grid.addWidget(btn0, 3, 1)
btnP = QPushButton(self)#+
btnP.setText('+')
btnP.clicked.connect(self.clickBtnP)
grid.addWidget(btnP, 2, 3)
btnM = QPushButton(self)#-
btnM.setText('-')
btnM.clicked.connect(self.clickBtnM)
grid.addWidget(btnM, 1, 3)
btnE = QPushButton(self)#=
btnE.setText('=')
btnE.clicked.connect(self.clickBtnE)
grid.addWidget(btnE, 3, 3)
btnC = QPushButton(self)#clear
btnC.setText('CE')
btnC.clicked.connect(self.clickBtnC)
grid.addWidget(btnC, 0, 3)
vbox = QVBoxLayout()
self.setLayout(vbox)
self.setWindowTitle('Calculator')
self.setGeometry(300, 300, 300, 200)
self.show()
def clickBtn1(self):
print(1, end='')
if self.a == 0:
self.n1 = self.n1 * 10 + 1
else:
self.n2 = self.n2 * 10 + 1
def clickBtn2(self):
print(2, end='')
if self.a == 0:
self.n1 = self.n1 * 10 + 2
else:
self.n2 = self.n2 * 10 + 2
def clickBtn3(self):
print(3, end='')
if self.a == 0:
self.n1 = self.n1 * 10 + 3
else:
self.n2 = self.n2 * 10 + 3
def clickBtn4(self):
print(4, end='')
if self.a == 0:
self.n1 = self.n1 * 10 + 4
else:
self.n2 = self.n2 * 10 + 4
def clickBtn5(self):
print(5, end='')
if self.a == 0:
self.n1 = self.n1 * 10 + 5
else:
self.n2 = self.n2 * 10 +5
def clickBtn6(self):
print(6, end='')
if self.a == 0:
self.n1 = self.n1 * 10 + 6
else:
self.n2 = self.n2 * 10 + 6
def clickBtn7(self):
print(7, end='')
if self.a == 0:
self.n1 = self.n1 * 10 + 7
else:
self.n2 = self.n2 * 10 + 7
def clickBtn8(self):
print(8, end='')
if self.a == 0:
self.n1 = self.n1 * 10 + 8
else:
self.n2 = self.n2 * 10 + 8
def clickBtn9(self):
print(9, end='')
if self.a == 0:
self.n1 = self.n1 * 10 + 9
else:
self.n2 = self.n2 * 10 + 9
def clickBtn0(self):
print(0, end='')
if self.a == 0:
self.n1 = self.n1 * 10
else:
self.n2 = self.n2 * 10
def clickBtnP(self):
print('+', end='')
self.a = 1
def clickBtnM(self):
print('-', end='')
self.a = 3
def clickBtnE(self):
print('=', end='')
if self.a == 1:
print(self.n1 + self.n2, end='')
self.n1 += self.n2
elif self.a == 3:
print(self.n1 - self.n2, end='')
self.n1 -= self.n1
self.n2 = 0
def clickBtnC(self):
print('')
self.n1 = 0
self.n2 = 0
self.a = 0
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
top of page
실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
2024.11.22
2024.11.22
댓글 0개
좋아요
댓글(0)
bottom of page