Search code examples
pythonpython-3.xgtk3glade

Creating modifiable GtkScale object in Glade


I'm trying to develop a simple GTK application with gtkme (1.3.5) library and I'm having trouble using GtkScale object in Glade (3.20.0-1). I created one of them and it's defined in .glade file by default as:

<object class="GtkScale">
    <property name="visible">True</property>
    <property name="can_focus">True</property>
    <property name="round_digits">1</property>
</object>

Yet the way it appears in Glade and my application makes it unusable, even after messing around with all of the available settings:

Not working GTK Scales object

I expected to see something like volume bars in pavucontrol instead:

Pavucontrol scales object

After messing around with the code from this thread I learned that changing object to <widget class="GtkHScale" id="hscale1"> or adding <property name="update_policy">GTK_UPDATE_CONTINUOUS</property> doesn't change anything.


Solution

  • To get a completely working Scale, you need to define two elements: A Gtk.Adjustment (which does the math work and defines limits) and a Gtk.Scale which shows the element on the Scale. Without an Adjustment, the Scale shows as in your image.

    So, in Glade, you need to define those two elements. The Adjustment will show as a non-visible element (see below), apart from the elements in the actual widget tree. The Scale will show initially as you've seen, and has to be connected to the Adjustment to actually show something. Adjustment and Scale are in different 'widget sections' in Glade, which might not be obvious:

    enter image description here

    and it will appear in the widget tree like this: enter image description here

    In the Scale's properties, you can select the Adjustment you want to assign to it. Of course you can do this also at run-time. The code to 'manually' make a working Scale is this (i.e. without Glade, but directly in your Python code):

        adj = Gtk.Adjustment(100.0, 0.0, 150.0, 2.0, 10.0, 10.0)
        scale = Gtk.Scale()
        scale.set_orientation(Gtk.Orientation.HORIZONTAL)
        scale.set_adjustment(adj)
    

    You can define the Adjustment in Glade (or add it later) and the same goes for the Scale widget (though it's more logical to do that in Glade anyway.

    Added references: