Hi All,

Mark Summerfield's  "Rapid GUI Programming with Python and Qt" will be 
known to most on this list. It is more than a fine book - its a great 
book!

I could not resist the temptation to run some of the book examples 
with PySide.  A cute example of "Expression Evaluator in 30 Lines" is 
a stunning example and the main adjustment to run it with PySide was 
the slight change of signal-slot syntax.  

All examples in the book are under GPL, so they are free to share and 
to modify.  Of course, for publication I would like to get author's 
permission which I have not asked for as yet.  I feel confident that 
Mark would not object for it to be mailed in a mailing list, 
particularly since he contributed to PySide by taking part in at least 
one psep.

The little program evaluates many kinds of mathematical expressions in 
a user friendly format and it is only some 30 lines long! On my PC the 
program runs in a konsole without hiccups.  In IDLE, it runs once and 
then complains with a claim that QApplication is still running.  In 
Eric it did not run because the unicode info was not accepted by 
QApplication.  From reading the list, I believe that this glitch has 
been taken care of.

I attach listing of the program and an image of its GUI.  It does the 
evaluation on detecting <E nter> click.  To avoid confusion, I 
"christened" the program evaluator.py.

Enjoy!

OldAl. 
-- 
Algis Kabaila
http://akabaila.pcug.org.au/ 
#!/usr/bin/env python
'''evaluator.py - expression evaluator'''

DEFAULT = 0
# default = 1 -> use PyQt, else use PySide

if DEFAULT:    
    from PyQt4.QtGui  import * 
    from PyQt4.QtCore import *
else:
    from PySide.QtGui  import * 
    from PySide.QtCore import *

#from __future__ import division

import sys
from math import *

class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = QTextBrowser()
        self.lineedit = QLineEdit('Type an expression and press Enter')
        self.lineedit.selectAll()
        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.lineedit)
        self.setLayout(layout)
        self.lineedit.setFocus()
        if DEFAULT:  # PyQt
            self.lineedit.returnPressed.connect(self.updateUi)
        else:    # PySide
            self.lineedit.returnPressed.connect(self.updateUi)
        self.setWindowTitle('Calculate')

    def updateUi(self):
        try:
            text = unicode(self.lineedit.text())
            self.browser.append('%s = <b>%s</b>' % (text, eval(text)))
        except:
            self.browser.append('<font color=red>%s is invalid!</font>' % 
                                (text))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()

<<attachment: snapshotEvaluator.png>>

_______________________________________________
PySide mailing list
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to