Archive for July, 2008
2-D Slider – GTK Widget
Today I build my first true GTK widget. Since written in Python, it’s not fitting in all GTK programs, but still I think some people might find it useful.
The Idea is to have a widget that can change two values at the same time. This comes in handy whenever a program has two numeric inputs that are manipulating one result. Users will not have to jump from one input element to another, but can use a diagram-like area to set values using an X and an Y axis.

When the widget is created, the programmer can select which ranges X and Y shall be in. The 2-D area will then displays a grid to show where values will be placed. And while the mouse is hovering over the grid, a preview dot will mark the nearest available value pair. If a coordinate is selected (by pressing the mouse button, or dragging the mouse over the field) an event will be generated, to inform the rest of the program about new values (X, Y, or both).
The Python sources can be found at the SVN repository of the #neo1973-germany developers Homepage. The following code snippet shows how to use a slider_2d widget and how to connect two callback functions to the “value_changed” events.
import slider_2d
...
def x_changed_callback(new_x_value):
value_x_label.set_text("Value X: %s" % new_x_value)
def y_changed_callback(new_y_value):
value_y_label.set_text("Value Y: %s" % new_y_value)
value_x_label = gtk.Label("Value X: none")
value_y_label = gtk.Label("Value Y: none")
slider_2d = slider_2d.slider_2d(x_range = (0, 30), y_range = (0, 10))
slider_2d.connect('x_value_changed_event', x_changed_callback)
slider_2d.connect('y_value_changed_event', y_changed_callback)
...
No comments