We will use a calculator widget that is already in linuxcnc (master)
I found this calculator widget on the net: http://sraji.wordpress.com/2009/09/08/calculator-using-python-and-glade/
I changed the keys a a bit for my use and switched to glade builder from libraryglade
here is the source code: upload:custom_calculator.zip
I apologize - this program uses leading tabs instead of spaces -that sucks but thats what the original program used.
If you run this with: python newcalculatorglade.py
It will display a calculator and pushing keys will change the display and pushing equals will do the math.
| This is the pre amble to set things up|
The first line lets you run this program from the gui by clicking on it
Importing sys and math allows system calls and math functions
importing pygtk actually seems not necessary - you could remove it.
importing gtk is necessary
If importing gtk fails an error message is printed and the program ends
| Here we initialize the class |
The class name is the generic name for our widget - Calculator
the eval_string is a variable used through out the program
gladefile is the path/name of the GLADE file we have produced for this widget. In this case it assumes that the glade file is in the path that the program was launched from which is probably not a good assumption.Gtk.builder is the method that builds our widget from the glade file. Then we write a dictionary of the signals and corresponding function calls. The signals were picked in the GLADE editor. Using a dictionary is one way of connecting signals- another common one seen is letting GLADE autoconnect them. If that is done the function calls names are the same as the signalnames,
Then we connect the signals. At the end we tell builder to find the window_calculator object in the GLADE file and show it
|We skipped a few lines here.|
here is the code that the signals from the calculator buttons call
.At the bottom is the funny if __name__ = __main__ command. This lets the program know if it is launched stand alone to run the commands under it. other wise it will just call the initialization code. The cal = Calculator() is the instantiation command, it creates an object of class Calculator, then the next command calls gtk.main() so it listens for signals from the buttons
|The arrow points to code that finds the path to where the program actually is rather then where it was called from. This is used later to load the glade file, which is assumed in the same folder as this program |
The first purple box shows the Gobject code for some attributes that we would like to be able to change in the glade editor
The second shows some new variables - note font and is_editable are the same as the Gobject attributes names -
|This arrow points to where we re-parent out widget to our Vbox|
The rest of the code is the functions to get and set the attributes
|These are the functions that Gobject calls when attributes changes|
Note that when when are attributes are set we call the appropriate function to actually do the work.
In the main function (used for testing) note that you can set the attributes directly by calling their function directly.