import sys
from PyQt5.QtWidgets import (QApplication, QPushButton, QWidget, QGridLayout, QLineEdit, QTextEdit, QVBoxLayout, QHBoxLayout)
from PyQt5.QtGui import *
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.X = 0
self.te = ''
def initUI(self):
hbox = QHBoxLayout()
self.y = QTextEdit()
self.y.setStyleSheet('background-color: rgb(50, 50, 50); color: white')
hbox.addWidget(self.y)
vbox = QVBoxLayout()
self.x = QLineEdit()
self.x.setStyleSheet('background-color: rgb(50, 50, 50); color: white; font: 18pt')
vbox.addWidget(self.x)
self.x.setText('0')
grid = QGridLayout()
for i in range(4):
for j in range(3):
if i * 3 + j >= 10:
break
btn = QPushButton(str(9 - (i * 3 + j)))
btn.setCheckable(False)
btn.clicked.connect(self.onChanged)
btn.setStyleSheet('QPushButton {background-color: rgb(110, 110, 110); color: white; font: 17pt}')
btn.toggle()
if 9 - (i * 3 + j) == 0:
grid.addWidget(btn, 4, 0)
else:
grid.addWidget(btn, i + 1, 2 - j)
btn1 = QPushButton('+')
btn1.setCheckable(False)
btn1.clicked.connect(self.plus)
btn1.setStyleSheet('QPushButton {background-color: rgb(230, 167, 99); color: white; font: 17pt}')
btn1.toggle()
grid.addWidget(btn1, 3, 3)
btn2 = QPushButton('-')
btn2.setCheckable(False)
btn2.clicked.connect(self.minus)
btn2.setStyleSheet('QPushButton {background-color: rgb(230, 167, 99); color: white; font: 17pt}')
btn2.toggle()
grid.addWidget(btn2, 2, 3)
btn3 = QPushButton('x')
btn3.setCheckable(False)
btn3.clicked.connect(self.multiple)
btn3.setStyleSheet('QPushButton {background-color: rgb(230, 167, 99); color: white; font: 17pt}')
btn3.toggle()
grid.addWidget(btn3, 1, 3)
btn4 = QPushButton('÷')
btn4.setCheckable(False)
btn4.clicked.connect(self.divide)
btn4.setStyleSheet('QPushButton {background-color: rgb(230, 167, 99); color: white; font: 17pt}')
btn4.toggle()
grid.addWidget(btn4, 0, 3)
btn5 = QPushButton('=')
btn5.setCheckable(False)
btn5.setStyleSheet('QPushButton {background-color: rgb(230, 167, 99); color: white; font: 17pt}')
btn5.toggle()
btn5.clicked.connect(self.equal)
grid.addWidget(btn5, 4, 3)
btn6 = QPushButton('AC')
btn6.setCheckable(False)
btn6.setStyleSheet('QPushButton {background-color: rgb(80, 80, 80); color: white; font: 17pt}')
btn6.toggle()
btn6.clicked.connect(self.reset)
grid.addWidget(btn6, 0, 0)
btn7 = QPushButton('+/-')
btn7.setCheckable(False)
btn7.setStyleSheet('QPushButton {background-color: rgb(80, 80, 80); color: white; font: 17pt}')
btn7.toggle()
btn7.clicked.connect(self.sign)
grid.addWidget(btn7, 0, 1)
btn8 = QPushButton('⌫')
btn8.setCheckable(False)
btn8.setStyleSheet('QPushButton {background-color: rgb(80, 80, 80); color: white; font: 17pt}')
btn8.toggle()
btn8.clicked.connect(self.delete)
grid.addWidget(btn8, 0, 2)
vbox.addLayout(grid)
hbox.addLayout(vbox)
self.setLayout(hbox)
self.setWindowTitle('calculator')
pal = QPalette()
pal.setColor(QPalette.Background, QColor(50, 50, 50))
self.setAutoFillBackground(True)
self.setPalette(pal)
self.setGeometry(300, 300, 300, 200)
self.show()
def onChanged(self):
btn = self.sender()
txt = int(self.x.text())
txt *= 10
txt += int(btn.text())
self.x.setText(str(txt))
def plus(self):
self.X = 1
self.number = int(self.x.text())
self.x.setText('0')
def minus(self):
self.X = 2
self.number = int(self.x.text())
self.x.setText('0')
def multiple(self):
self.X = 3
self.number = int(self.x.text())
self.x.setText('0')
def divide(self):
self.X = 4
self.number = int(self.x.text())
self.x.setText('0')
def equal(self):
num = int(self.x.text())
if self.X == 1:
self.x.setText(str(self.number + num))
self.te = (str(self.number) + '+' + str(num) + '=' + str(self.number + num))
elif self.X == 2:
self.x.setText(str(self.number - num))
self.te = (str(self.number) + '-' + str(num) + '=' + str(self.number - num))
elif self.X == 3:
self.x.setText(str(self.number * num))
self.te = (str(self.number) + 'x' + str(num) + '=' + str(self.number * num))
elif self.X == 4:
if num == 0:
self.x.setText(str(self.number))
self.te = (str(self.number) + '÷' + str(num) + '=' + str(self.number))
else:
self.x.setText(str(self.number // num))
self.te = (str(self.number) + '÷' + str(num) + '=' + str(self.number // num))
self.y.setText(self.te)
self.number = 0
self.X = 0
def reset(self):
self.x.setText('0')
self.number = 0
def sign(self):
self.x.setText(str(-(int(self.x.text()))))
def delete(self):
self.x.setText(str((int(self.x.text())) // 10))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())top of page

실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
2022.7.16.
2022.7.16.
댓글 0개
좋아요
댓글(0)
더 이상 게시물에 대한 댓글 기능이 지원되지 않습니다. 자세한 사항은 사이트 소유자에게 문의하세요.
bottom of page


