Created
February 17, 2017 10:15
-
-
Save ishanjain28/e71c053eb2300110793afbae836592c0 to your computer and use it in GitHub Desktop.
Simple GUI Calculator made with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt4 import QtGui | |
import sys | |
import math | |
class Calc(QtGui.QMainWindow): | |
def __init__(self): | |
super(Calc, self).__init__() | |
self.DrawLayout() | |
self.DrawTextField() | |
self.DrawButtons('1', 0, 1) | |
self.DrawButtons('2', 1, 1) | |
self.DrawButtons('3', 2, 1) | |
self.DrawButtons('+', 3, 1) | |
self.DrawButtons('4', 0, 2) | |
self.DrawButtons('5', 1, 2) | |
self.DrawButtons('6', 2, 2) | |
self.DrawButtons('-', 3, 2) | |
self.DrawButtons('7', 0, 3) | |
self.DrawButtons('8', 1, 3) | |
self.DrawButtons('9', 2, 3) | |
self.DrawButtons('*', 3, 3) | |
self.DrawButtons('C', 0, 4) | |
self.DrawButtons('0', 1, 4) | |
self.DrawButtons('=', 2, 4) | |
self.DrawButtons('/', 3, 4) | |
self.show() | |
def DrawLayout(self): | |
self.setGeometry(100, 100, 700, 700) | |
self.setWindowTitle('Calculator') | |
def DrawTextField(self): | |
self.textfield = QtGui.QLineEdit(' ', self) | |
self.textfield.move(50, 100) | |
self.textfield.resize(600, 70) | |
def DrawButtons(self, text, xDistance, yDistance): | |
btn = QtGui.QPushButton(text, self) | |
btn.move(160 + (100 * xDistance), 160 + (100 * yDistance)) | |
btn.resize(60, 50) | |
def AppendToTextField(): | |
if text == "C": | |
self.textfield.setText("") | |
elif text == "+" or text == "/" or text == "*" or text == "-": | |
op = self.textfield.text()[-1] | |
if not (op == "+" or op == "-" or op == "*" or op == "/"): | |
self.textfield.setText(self.textfield.text() + text) | |
elif text.isdigit(): | |
self.textfield.setText(self.textfield.text() + text) | |
elif text == "=": | |
textfieldData = self.textfield.text().replace(" ", "") | |
tempStack = [] | |
print(tempStack) | |
btn.clicked.connect(AppendToTextField) | |
app = QtGui.QApplication(sys.argv) | |
x = Calc() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment