Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [423]

By Root 1527 0

Line 9 matches the line read from stdin against a regular expression, and returns a match object as a result of matching. This object contains a method for accessing the subparts of the match. Line 21 converts the result of the division minutes[user]/60 to a string. This is done using two backquotes.

Developing a Calculator Using Python

In this section we look into developing a slightly more complicated application, which uses classes. The application is a reverse Polish notation calculator, and can be seen in Figure 21-1. For developing the graphical user interface, we use the Qt library, which is a C++ library wrapped for many different languages, such as Perl, Python, and Java.

Figure 21-1. A calculator developed in Python

The program consists of two classes: Display, which is the area displaying the numbers, and Calculator, which is a class for the calculator:

1 #!/usr/bin/python

2 import sys, string

3 from qt import *

4

5 class Display(QTextEdit):

6 def _ _init_ _( self, parent ):

7 QTextEdit._ _init_ _( self, parent )

8 self.setAlignment( Qt.AlignRight )

9 self.setReadOnly( 1 )

10

11 def pop( self ):

12 lines = self.paragraphs()

13 if lines = = 0:

14 return 0

15

16 res = QString.stripWhiteSpace(self.text( lines-1 ))

17 self.removeParagraph(lines-1)

18

19 if ( res.isEmpty() ):

20 return 0

21

22 return res.toFloat()[0]

23

24 def push( self, txt ):

25 self.append( `txt` )

26

27 class Calculator(QDialog):

28 # Constructor

29 def _ _init_ _(self, parent):

30 QDialog._ _init_ _(self, parent)

31 vlay = QVBoxLayout( self, 6 )

32 self.insertMode = 0

33

34 # Create display

35 self.edit = Display( self )

36 vlay.addWidget( self.edit )

37

38 # Create button array

39 index = 0

40 for txt in [ "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", \

41 "*", "C", "0", "Enter", "/" ]:

42 if (index%4) = = 0:

43 hlay = QHBoxLayout( vlay )

44 index = index+1

45

46 but = QPushButton( txt, self )

47 but.setAutoDefault(0)

48 QObject.connect( but, SIGNAL( "clicked()" ), self.buttonClicked )

49 hlay.addWidget( but )

50

51 # Function reacting on button clicks

52 def buttonClicked(self):

53 txt = self.sender().text() # Text on button pressed.

54 if txt = = "Enter":

55 self.insertMode = 0

56

57 elif txt in [ "+", "-", "*", "/" ]:

58 val1 = self.edit.pop()

59 val2 = self.edit.pop()

60 self.edit.push( self.evaluate( val2, val1, txt ) )

61 self.insertMode = 0

62

63 elif txt = = "C":

64 self.edit.pop()

65 self.insertMode = 0

66

67 else: # A number pressed.

68 if self.insertMode:

69 val = self.edit.pop()

70 else:

71 self.insertMode = 1

72 val = 0

73 val = val*10+ txt.toFloat()[0]

74 self.edit.push(val)

75

76 def evaluate( self, arg1, arg2, op ):

77 if ( op = = "+" ):

78 return arg1 + arg2

79 elif ( op = = "-" ):

80 return arg1 - arg2

81 elif ( op = = "*" ):

82 return arg1 * arg2

83 elif (op = = "/" ):

84 return arg1 / arg2

85

86 # Main

87 app=QApplication(sys.argv)

88 cal = Calculator(None)

89 cal.show()

90 app.exec_loop()

The code may at first look like a lot of work; on the other hand, with only 90 lines of code we have developed a working application with a graphical user interface. Most of the work is really handled by Qt, and since this is not a book on programming with Qt, we will not delve into these issues too much. Let's have a look at the code snippet by snippet.

Let's start where execution of our application starts — namely, at lines 86 through 90. Of course execution starts on line 1, but the first 85 lines merely defined the classes, which doesn't result in any code being executed.

Line 87 creates an instance of the class QApplication, in contrast to other languages such as Java or C++, you do not use the keyword new to create an instance — you simply name the class. The class QApplication comes from qt.py, which we include in a special way on line 3: we say that all symbols from that module should be included into our namespace, so we do not need to write qt.QApplication. Doing it this way is seldom a good idea, but it is

Return Main Page Previous Page Next Page

®Online Book Reader