[Home]GladeVCP Custom Widgets

LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org

Showing revision 20
Here we attempt to describe how to build a custom glade widget using python.
Note i am not a programmer by trade, more of a hacker, so what I so you works, but there may be better ways.
This is current as of June 2012 (under construction)

We will use a calculator widget that is already in linuxcnc (master)

1. Custom stand-alone GTK widget
1.1. What do we start with?
1.2. looking at the code
2. Custom GTK widget in glade editor


1. Custom stand-alone GTK widget

We will start with a regular custom widget program that uses GLADE to build the display but this widget is not included in the GLADE interface.

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.
upload:calculator.png 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.

1.1. What do we start with?

When building a custom widget we pick an object that is close to what we want and then add to it from there.(thats the calculator example)
In the case of a really custom widget - one where you want to draw things you pick a widget and 'override' its original function calls and make it do what you want.
here is an example for a clock using gtkDrawingArea?:
http://www.pygtk.org/articles/cairo-pygtk-widgets/cairo-pygtk-widgets.htm
http://www.pygtk.org/articles/cairo-pygtk-widgets/cairo-pygtk-widgets2.htm
here is another one - this one use the base class widget:
http://www.pygtk.org/articles/writing-a-custom-widget-using-pygtk/writing-a-custom-widget-using-pygtk.htm

1.2. looking at the code

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
upload:calculator_code1.png
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
upload:calculator_code2.png
We skipped a few lines here.
here is the code that the signals from the calculator buttons call<br.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 instantiation command it creates an object of class Caculator, then the next command calls gtk.main() so it listens for signals from the buttons
upload:calculator_code3.png

2. Custom GTK widget in glade editor

Now we want it to show up in the glade editor so we can add it to other containers.
The first problem is that by building it with the GLADE editor we had to add it to a top window.
A top window can not be put into anything else so we must re-parent the custom widget.
(This would not be true if we had built the widget without the GLADE editor.)

LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org
This page is read-only. Follow the BasicSteps to edit pages. | View other revisions | View current revision
Edited June 25, 2012 8:58 pm by Cmorley (diff)
Search:
Published under a Creative Commons License