import sys
import matplotlib.pyplot as plt
import numpy as np
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 = ''
self.F = 0
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')
self.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:
self.grid.addWidget(btn, 4, 0)
else:
self.grid.addWidget(btn, i + 1, 2 - j)
btn1 = QPushButton('+')
btn1.setCheckable(False)
btn1.clicked.connect(self.plus)
btn1.setStyleSheet('QPushButton {background-color: rgb(150, 150, 150); color: white; font: 17pt}')
btn1.toggle()
self.grid.addWidget(btn1, 3, 3)
btn2 = QPushButton('-')
btn2.setCheckable(False)
btn2.clicked.connect(self.minus)
btn2.setStyleSheet('QPushButton {background-color: rgb(150, 150, 150); color: white; font: 17pt}')
btn2.toggle()
self.grid.addWidget(btn2, 2, 3)
btn3 = QPushButton('x')
btn3.setCheckable(False)
btn3.clicked.connect(self.multiple)
btn3.setStyleSheet('QPushButton {background-color: rgb(150, 150, 150); color: white; font: 17pt}')
btn3.toggle()
self.grid.addWidget(btn3, 1, 3)
btn4 = QPushButton('÷')
btn4.setCheckable(False)
btn4.clicked.connect(self.divide)
btn4.setStyleSheet('QPushButton {background-color: rgb(150, 150, 150); color: white; font: 17pt}')
btn4.toggle()
self.grid.addWidget(btn4, 0, 3)
btn5 = QPushButton('=')
btn5.setCheckable(False)
btn5.setStyleSheet('QPushButton {background-color: rgb(150, 150, 150); color: white; font: 17pt}')
btn5.toggle()
btn5.clicked.connect(self.equal)
self.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)
self.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)
self.grid.addWidget(btn7, 0, 1)
btn8 = QPushButton('⌫')
btn8.setCheckable(False)
btn8.setStyleSheet('QPushButton {background-color: rgb(80, 80, 80); color: white; font: 13pt}')
btn8.toggle()
btn8.clicked.connect(self.delete)
self.grid.addWidget(btn8, 0, 2)
btn9 = QPushButton('GRAPH')
btn9.setCheckable(False)
btn9.setStyleSheet('font: 17pt}')
btn9.toggle()
btn9.clicked.connect(self.graph)
self.grid.addWidget(btn9, 5, 0)
btn10 = QPushButton('a')
btn10.setCheckable(False)
btn10.setStyleSheet('font: 17pt}')
btn10.toggle()
btn10.clicked.connect(self.A)
self.grid.addWidget(btn10, 5, 1)
btn11 = QPushButton('b')
btn11.setCheckable(False)
btn11.setStyleSheet('font: 17pt}')
btn11.toggle()
btn11.clicked.connect(self.B)
self.grid.addWidget(btn11, 5, 2)
vbox.addLayout(self.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):
if self.J == 1 and self.K == 1:
plt.style.use('_mpl-gallery')
x = np.linspace(-10, 10, 10)
y = x * 1
fig, ax = plt.subplots()
ax.plot(x, y * 0, 'k', x * 0, y, 'k')
x = np.linspace(-10, 10, 10)
y = x * self.numA + self.numB
ax.plot(x, y, 'r')
ax.set(xlim=(-10, 10), xticks=np.arange(-10, 11),
ylim=(-10, 10), yticks=np.arange(-10, 11))
plt.show()
self.F = 0
self.J = 0
self.K = 0
else:
num = int(self.x.text())
if self.X == 1:
self.x.setText(str(self.number + num))
self.te = self.te + '\n' + (str(self.number) + '+' + str(num) + '=' + str(self.number + num))
elif self.X == 2:
self.x.setText(str(self.number - num))
self.te = self.te + '\n' + (str(self.number) + '-' + str(num) + '=' + str(self.number - num))
elif self.X == 3:
self.x.setText(str(self.number * num))
self.te = self.te + '\n' + (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 = self.te + '\n' + (str(self.number) + '÷' + str(num) + '=' + str(self.number))
else:
self.x.setText(str(self.number // num))
self.te = self.te + '\n' + (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))
def graph(self):
self.F = 1
def A(self):
if self.F == 1:
self.numA = int(self.x.text())
self.te = self.te + '\n' +"a:" + str(self.numA)
self.J = 1
self.x.setText('0')
self.y.setText(self.te)
def B(self):
if self.F == 1:
self.numB = int(self.x.text())
self.te = self.te + '\n' + "b:" + str(self.numB)
self.K = 1
self.x.setText('0')
self.y.setText(self.te)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
top of page

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


