Skip to main content

Making a calculator in Python using pyface and raw Qt.

I wrote yesterday about making a calculator GUI using enthought's traitsUI package. Well, for fun, I rewrote the whole damn thing using Enthought's pyface package in raw Qt. The main difference is that we have lost the ability to automatically monitor the changes in the attributes i.e monitor when the buttons are being pressed. So, we have to now manually connect the QPushButton objects to functions that handle what to do when one of the buttons is pressed. And the layout of the buttons is a little harder to define as well. Well, harder for me because I still haven't been able to wrap my head around Qt.

Anyway, that's all for today I guess. Ohh, also, the 0 button isn't behaving well and I don't think the . button is either! I wanted to write unittests to automate checking whether or not the app/program is working as its supposed to. I'm still trying to learn how to write unittests, for GUI elements, after a failed attempt a couple of days back.

Again, the code was highlighted using this awesome website! And any comments/suggestions are welcome and highly appreciated! :)


#  Copyright (c) 2007, Enthought, Inc.
#  License: BSD Style.

# http://srinikom.github.io/pyside-docs/PySide/QtGui/QGridLayout.html#PySide.QtGui.PySide.QtGui.QGridLayout.addLayout
# http://zetcode.com/gui/qt4/layoutmanagement/


"""
Implementation of a TableEditor demo plugin for Traits UI demo program

This demo shows the full behavior of a straightforward TableEditor.  Only
one style of TableEditor is implemented, so that is the one shown.
"""

# Import statements:

import sys

from traits.api import Int, Float
from pyface.qt.QtGui import *
from pyface.qt.QtCore import QCoreApplication
#from pyface.qt import QString

# The class to be edited with the TableEditor:
class Application ():
    compute_box = 0.0 
    temp = 0.0
    count = 0
    dot = -1

    app = QApplication(sys.argv)
    window = QWidget()
    textEdit = QTextEdit()
    textEdit.setReadOnly(True)
    textEdit.setText(unicode(compute_box))

    nine_button = QPushButton('9')
    eight_button = QPushButton('8')
    seven_button = QPushButton('7')
    six_button = QPushButton('6')
    five_button = QPushButton('5')
    four_button = QPushButton('4')
    three_button = QPushButton('3')
    two_button = QPushButton('2')
    one_button = QPushButton('1')

    equals_button = QPushButton('=')
    add_button = QPushButton('+')
    subtract_button = QPushButton('-')
    mul_button = QPushButton('*')
    div_button = QPushButton('/')
    zero_button = QPushButton('0')
    dot_button = QPushButton('.')

    clr_button = QPushButton('clr')

    def nine_button_fired(self):
        if self.dot == -1:
            self.compute_box = self.compute_box*10 +9.0
            self.textEdit.setText(unicode(self.compute_box))
        else:
            self.compute_box = (self.compute_box*10**(self.dot) +9.0)/10**(self.dot)
            self.dot +=1
            self.textEdit.setText(unicode(self.compute_box))

    def eight_button_fired(self):
        if self.dot == -1:
            self.compute_box = self.compute_box*10 +8.0
            self.textEdit.setText(unicode(self.compute_box))
        else:
            self.compute_box = (self.compute_box*10**(self.dot) +8.0)/10**(self.dot)
            self.dot +=1
            self.textEdit.setText(unicode(self.compute_box))

    def seven_button_fired(self):
        if self.dot == -1:
            self.compute_box = self.compute_box*10 +7.0
            self.textEdit.setText(unicode(self.compute_box))
        else:
            self.compute_box = (self.compute_box*10**(self.dot) +7.0)/10**(self.dot)
            self.dot +=1
            self.textEdit.setText(unicode(self.compute_box))

    def six_button_fired(self):
        if self.dot == -1: 
            self.compute_box = self.compute_box*10 +6.0
            self.textEdit.setText(unicode(self.compute_box))
        else:
            self.compute_box = (self.compute_box*10**(self.dot) +6.0)/10**(self.dot)
            self.dot +=1
            self.textEdit.setText(unicode(self.compute_box))

    def five_button_fired(self):
        if self.dot == -1: 
            self.compute_box = self.compute_box*10 +5.0
            self.textEdit.setText(unicode(self.compute_box))
        else:
            self.compute_box = (self.compute_box*10**(self.dot) +5.0)/10**(self.dot)
            self.dot +=1
            self.textEdit.setText(unicode(self.compute_box))

    def four_button_fired(self):
        if self.dot == -1: 
            self.compute_box = self.compute_box*10 +4.0
            self.textEdit.setText(unicode(self.compute_box))
        else:
            self.compute_box = (self.compute_box*10**(self.dot) +4.0)/10**(self.dot)
            self.dot +=1
            self.textEdit.setText(unicode(self.compute_box))

    def three_button_fired(self):
        if self.dot == -1: 
            self.compute_box = self.compute_box*10 +3.0
            self.textEdit.setText(unicode(self.compute_box))
        else:
            self.compute_box = (self.compute_box*10**(self.dot) +3.0)/10**(self.dot)
            self.dot +=1
            self.textEdit.setText(unicode(self.compute_box))

    def two_button_fired(self):
        if self.dot == -1: 
            self.compute_box = self.compute_box*10 +2.0
            self.textEdit.setText(unicode(self.compute_box))
        else:
            self.compute_box = (self.compute_box*10**(self.dot) +2.0)/10**(self.dot)
            self.dot +=1
            self.textEdit.setText(unicode(self.compute_box))

    def one_button_fired(self):
        if self.dot == -1: 
            self.compute_box = self.compute_box*10 +1.0
            self.textEdit.setText(unicode(self.compute_box))
        else:
            self.compute_box = (self.compute_box*10**(self.dot) +1.0)/10**(self.dot)
            self.dot +=1
            self.textEdit.setText(unicode(self.compute_box))

    def zero_button_fired(self):
        # does some weird shit!
        if self.dot == -1: 
            self.compute_box = self.compute_box*10
            self.textEdit.setText(unicode(self.compute_box))
#        else:
            # dunno what to do when we want to add 
            # after zero!

    def dot_button_fired(self):
            self.dot = 1

    def eval_expr(self):
        if self.count == 1:
            self.compute_box += self.temp
        elif self.count == -1:
            self.compute_box = self.temp -self.compute_box
        elif self.count == 2:
            self.compute_box = self.compute_box*self.temp
        elif self.count == -2:
            self.compute_box = self.temp/self.compute_box
        self.dot = -1
        self.count = 0
        self.textEdit.setText(unicode(self.compute_box))
        self.temp = 0.0

    def add_button_fired(self):
        self.temp = self.compute_box
        self.compute_box = 0.0
        self.count = 1
        self.dot = -1
        self.textEdit.setText(unicode(self.compute_box))

    def subtract_button_fired(self):
        self.temp = self.compute_box
        self.compute_box = 0.0
        self.count = -1
        self.dot = -1
        self.textEdit.setText(unicode(self.compute_box))

    def mul_button_fired(self):
        self.temp = self.compute_box
        self.compute_box = 0.0
        self.count = 2
        self.dot = -1
        self.textEdit.setText(unicode(self.compute_box))

    def div_button_fired(self):
        self.temp = self.compute_box
        self.compute_box = 0.0
        self.count = -2
        self.dot = -1
        self.textEdit.setText(unicode(self.compute_box))

    def clr_button_fired(self):
        self.temp = 0.0
        self.compute_box = 0.0
        self.count = 0
        self.dot =-1 
        self.textEdit.setText(unicode(self.compute_box))

    def main(self):
        self.nine_button.clicked.connect(self.nine_button_fired)
        self.eight_button.clicked.connect(self.eight_button_fired)
        self.seven_button.clicked.connect(self.seven_button_fired)
        self.six_button.clicked.connect(self.six_button_fired)
        self.five_button.clicked.connect(self.five_button_fired)
        self.four_button.clicked.connect(self.four_button_fired)
        self.three_button.clicked.connect(self.three_button_fired)
        self.two_button.clicked.connect(self.two_button_fired)
        self.one_button.clicked.connect(self.one_button_fired)
        self.zero_button.clicked.connect(self.zero_button_fired)

        self.mul_button.clicked.connect(self.mul_button_fired)
        self.add_button.clicked.connect(self.add_button_fired)
        self.subtract_button.clicked.connect(self.subtract_button_fired)
        self.div_button.clicked.connect(self.div_button_fired)
        self.dot_button.clicked.connect(self.dot_button_fired)
        self.clr_button.clicked.connect(self.clr_button_fired) 
        self.equals_button.clicked.connect(self.eval_expr)

        textlayout = QGridLayout()
        layout = QGridLayout()
        #layout.addWidget(window,3,3)
        mainlayout = QVBoxLayout()
 textlayout.addWidget(self.textEdit)
 layout.addWidget(self.one_button,1,1)
 layout.addWidget(self.two_button,1,2)
 layout.addWidget(self.three_button,1,3)
        layout.addWidget(self.four_button,2,1)
        layout.addWidget(self.five_button,2,2)
        layout.addWidget(self.six_button,2,3)
 layout.addWidget(self.seven_button,3,1)
        layout.addWidget(self.eight_button,3,2)
        layout.addWidget(self.nine_button,3,3)
        layout.addWidget(self.zero_button,4,2)

 layout.addWidget(self.mul_button,4,1)
        layout.addWidget(self.add_button,5,1)
        layout.addWidget(self.div_button,4,3)
        layout.addWidget(self.dot_button,5,2)
 layout.addWidget(self.subtract_button,5,3)
        layout.addWidget(self.clr_button,6,1)
        layout.addWidget(self.equals_button,6,3)

        mainlayout.addLayout(textlayout)
        mainlayout.addLayout(layout)
 self.window.setLayout(mainlayout)
        self.window.setWindowTitle("Calculator : Poruri Sai Rahul")

 self.window.show()

 return self.app.exec_()

# Create the demo:
demo = Application()

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
    demo.main()


Popular posts from this blog

Animation using GNUPlot

Animation using GNUPlotI've been trying to create an animation depicting a quasar spectrum moving across the 5 SDSS pass bands with respect to redshift. It is important to visualise what emission lines are moving in and out of bands to be able to understand the color-redshift plots and the changes in it.
I've tried doing this using the animate function in matplotlib, python but i wasn't able to make it work - meaning i worked on it for a couple of days and then i gave up, not having found solutions for my problems on the internet.
And then i came across this site, where the gunn-peterson trough and the lyman alpha forest have been depicted - in a beautiful manner. And this got me interested in using js and d3 to do the animations and make it dynamic - using sliders etc.
In the meanwhile, i thought i'd look up and see if there was a way to create animations in gnuplot and whoopdedoo, what do i find but nirvana!

In the image, you see 5 static curves and one dynam…

on MOOCs.

For those of you who don't know, MOOC stands for Massively Open Online Course.

The internet is an awesome thing. It's making education free for all. Well, mostly free. But it's surprising at the width and depth of courses being offered online. And it looks like they are also having an impact on students, especially those from universities that are not top ranked. Students in all parts of the world can now get a first class education experience, thanks to courses offered by Stanford, MIT, Caltech, etc.

I'm talking about MOOCs because one of my new year resolutions is to take online courses, atleast 2 per semester (6 months). And I've chosen the following two courses on edX - Analyzing Big Data with Microsoft R Server and Data Science Essentials for now. I looked at courses on Coursera but I couldn't find any which was worthy and free. There are a lot more MOOC providers out there but let's start here. And I feel like the two courses are relevant to where I …

On programmers.

I just watched this brilliant keynote today. It's a commentary on Programmers and the software development industry/ecosystem as a whole.



I am not going to give you a tl;dr version of the talk because it is a talk that I believe everyone should watch, that everyone should learn from. Instead, I am going to give my own parallel-ish views on programmers and programming.
As pointed out in the talk, there are mythical creatures in the software development industry who are revered as gods. Guido Van Rossum, the creator of Python, was given the title Benevolent Dictator For Life (BDFL). People flock around the creators of popular languages or libraries. They are god-like to most programmers and are treated like gods. By which, I mean to say, we assume they don't have flaws. That they are infallible. That they are perfect.
And alongside this belief in the infallibility of these Gods, we believe that they were born programmers. That programming is something that people are born wit…