[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

dictionary is thrown up in the main window for use in larger expressions. You can use
this as an escape mechanism to employ external tools in your calculations. For instance,
you can import and use functions coded in Python or C within these pop ups. The
current value in the main calculator window is stored in newly opened command-line
pop ups, too, for use in typed expressions.


PyCalc supports integers (unlimited precision), negatives, and floating-point numbers
just because Python does. Individual operands and expressions are still evaluated with
the eval built-in, which calls the Python parser/interpreter at runtime. Variable names
can be assigned and referenced in the main window with the letter, =, and “eval” keys;
they are assigned in the calculator’s namespace dictionary (more complex variable
names may be typed in command-line pop ups). Note the use of pi in the history
window: PyCalc preimports names in the math and random modules into the namespace
where expressions are evaluated.


Evaluating expressions with stacks


Now that you have the general idea of what PyCalc does, I need to say a little bit about
how it does what it does. Most of the changes in this calculator involve managing the
expression display and evaluating expressions. PyCalc is structured as two classes:


The CalcGui class
Manages the GUI itself. It controls input events and is in charge of the main win-
dow’s display field at the top. It doesn’t evaluate expressions, though; for that, it


Figure 19-5. PyCalc calculator with some of its pop ups


PyCalc: A Calculator Program/Object | 1465
Free download pdf