Created
March 25, 2020 18:44
-
-
Save cpascual/c1c5af849c3cc7164096e5936426b705 to your computer and use it in GitHub Desktop.
Simple calculator made with pyqt5 (for basic qt training)
This file contains hidden or 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
import os | |
import sys | |
from PyQt5 import Qt, uic | |
class MyCalc(Qt.QWidget): | |
"""A specialized QLineEdit (implemented using signal connections declared | |
in the loaded .ui file) | |
""" | |
def __init__(self, parent=None): | |
# call the parent class init | |
Qt.QWidget.__init__(self, parent=parent) | |
# load the .ui file | |
uic.loadUi(os.path.join(os.path.dirname(__file__), "mycalc.ui"), self) | |
# initialize the _reset_needed flag to True to remove the initial 0 | |
self._reset_needed = True | |
# connect the buttons to the slots | |
for b in ( | |
self.b0, | |
self.b1, | |
self.b2, | |
self.b3, | |
self.b4, | |
self.b5, | |
self.b6, | |
self.b7, | |
self.b8, | |
self.b9, | |
self.bplus, | |
self.bminus, | |
self.bmult, | |
self.bdiv, | |
self.bdot, | |
): | |
b.clicked.connect(self.onKeyClicked) | |
self.beq.clicked.connect(self.onEqClicked) | |
self.bclear.clicked.connect(self.onClearClicked) | |
def onKeyClicked(self): | |
# do not accept entering more than 15 digits | |
if len(self.label.text()) > 15: | |
return | |
# before appending new text, reset the existing one if required | |
if self._reset_needed: | |
self.label.setText("") | |
self._reset_needed = False | |
# append the corresponding button text to the text currently displayed | |
text = self.label.text() + self.sender().text() | |
self.label.setText(text) | |
def onEqClicked(self): | |
try: | |
# use python eval to evaluate the expression being displayed | |
text = "{:g}".format(eval(self.label.text())) | |
if len(text) > 15: | |
raise OverflowError | |
except: | |
text = "Error" | |
self._reset_needed = True | |
self.label.setText(text) | |
def onClearClicked(self): | |
self.label.setText("0") | |
self._reset_needed = True | |
if __name__ == "__main__": | |
# Initialize a Qt application (Qt will crash if you do not do this first) | |
app = Qt.QApplication(sys.argv) | |
# instantiate the widget | |
w = MyCalc() | |
w.setWindowTitle("MyCalc") | |
# show it (if you do not show the widget, it won't be visible) | |
w.show() | |
# Initialize the Qt event loop (and exit when we close the app) | |
sys.exit(app.exec_()) |
This file contains hidden or 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
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>Form</class> | |
<widget class="QWidget" name="Form"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>358</width> | |
<height>196</height> | |
</rect> | |
</property> | |
<property name="windowTitle"> | |
<string>Form</string> | |
</property> | |
<layout class="QVBoxLayout" name="verticalLayout"> | |
<item> | |
<widget class="QLabel" name="label"> | |
<property name="sizePolicy"> | |
<sizepolicy hsizetype="Preferred" vsizetype="Fixed"> | |
<horstretch>0</horstretch> | |
<verstretch>0</verstretch> | |
</sizepolicy> | |
</property> | |
<property name="maximumSize"> | |
<size> | |
<width>16777215</width> | |
<height>300</height> | |
</size> | |
</property> | |
<property name="font"> | |
<font> | |
<pointsize>18</pointsize> | |
</font> | |
</property> | |
<property name="frameShape"> | |
<enum>QFrame::Panel</enum> | |
</property> | |
<property name="frameShadow"> | |
<enum>QFrame::Sunken</enum> | |
</property> | |
<property name="midLineWidth"> | |
<number>1</number> | |
</property> | |
<property name="text"> | |
<string>0</string> | |
</property> | |
<property name="scaledContents"> | |
<bool>false</bool> | |
</property> | |
<property name="alignment"> | |
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> | |
</property> | |
</widget> | |
</item> | |
<item> | |
<layout class="QGridLayout" name="gridLayout"> | |
<item row="0" column="0"> | |
<widget class="QPushButton" name="b1"> | |
<property name="text"> | |
<string>1</string> | |
</property> | |
</widget> | |
</item> | |
<item row="0" column="1"> | |
<widget class="QPushButton" name="b2"> | |
<property name="text"> | |
<string>2</string> | |
</property> | |
</widget> | |
</item> | |
<item row="0" column="2"> | |
<widget class="QPushButton" name="b3"> | |
<property name="text"> | |
<string>3</string> | |
</property> | |
</widget> | |
</item> | |
<item row="1" column="0"> | |
<widget class="QPushButton" name="b4"> | |
<property name="text"> | |
<string>4</string> | |
</property> | |
</widget> | |
</item> | |
<item row="1" column="1"> | |
<widget class="QPushButton" name="b5"> | |
<property name="text"> | |
<string>5</string> | |
</property> | |
</widget> | |
</item> | |
<item row="1" column="2"> | |
<widget class="QPushButton" name="b6"> | |
<property name="text"> | |
<string>6</string> | |
</property> | |
</widget> | |
</item> | |
<item row="2" column="0"> | |
<widget class="QPushButton" name="b7"> | |
<property name="text"> | |
<string>7</string> | |
</property> | |
</widget> | |
</item> | |
<item row="2" column="1"> | |
<widget class="QPushButton" name="b8"> | |
<property name="text"> | |
<string>8</string> | |
</property> | |
</widget> | |
</item> | |
<item row="2" column="2"> | |
<widget class="QPushButton" name="b9"> | |
<property name="text"> | |
<string>9</string> | |
</property> | |
</widget> | |
</item> | |
<item row="3" column="1"> | |
<widget class="QPushButton" name="b0"> | |
<property name="text"> | |
<string>0</string> | |
</property> | |
</widget> | |
</item> | |
<item row="3" column="0"> | |
<widget class="QPushButton" name="bdot"> | |
<property name="text"> | |
<string>.</string> | |
</property> | |
</widget> | |
</item> | |
<item row="3" column="3"> | |
<widget class="QPushButton" name="bdiv"> | |
<property name="text"> | |
<string>/</string> | |
</property> | |
</widget> | |
</item> | |
<item row="0" column="3"> | |
<widget class="QPushButton" name="bplus"> | |
<property name="text"> | |
<string>+</string> | |
</property> | |
</widget> | |
</item> | |
<item row="2" column="3"> | |
<widget class="QPushButton" name="bmult"> | |
<property name="text"> | |
<string>*</string> | |
</property> | |
</widget> | |
</item> | |
<item row="1" column="3"> | |
<widget class="QPushButton" name="bminus"> | |
<property name="text"> | |
<string>-</string> | |
</property> | |
</widget> | |
</item> | |
<item row="3" column="2"> | |
<widget class="QPushButton" name="bclear"> | |
<property name="text"> | |
<string>C</string> | |
</property> | |
</widget> | |
</item> | |
<item row="4" column="2" colspan="2"> | |
<widget class="QPushButton" name="beq"> | |
<property name="text"> | |
<string>=</string> | |
</property> | |
</widget> | |
</item> | |
</layout> | |
</item> | |
</layout> | |
</widget> | |
<resources/> | |
<connections/> | |
</ui> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment