Search code examples
pythonpygtk

How to change size of VScale in GTK Python


I have added a gtk.VScale() to my GUI and the size is really small, but why ?

I have this code sample :

def __init__(self, parent, grid):
    self.parent = parent

    self.tooltips = gtk.Tooltips()

    self.ajustement = gtk.Adjustment(0.0, 0.0, 101.0, 0.1, 1.0, 1.0)
    self.scaleH = gtk.VScale(self.ajustement)

    self.bt_lumiere = gtk.ToggleButton()
    self.bt_lumiere.set_active(False)
    self.bt_lumiere.set_image(gtk.image_new_from_file('data/icons/moon.jpg'))
    self.bt_lumiere.connect("pressed",self.on_changer_etat_lumiere)

    self.barreLumiere = BarreLuminosite(self)

    box = gtk.VBox(False,5)
    box.pack_start(self.bt_lumiere, True)
    box.pack_start(self.scaleH,True)

    grid.attach(self.align(box, padright=1, padleft=1), 1,2,1,5)

def align(self, widget, xalign=0, yalign=0.5, padtop=0, padbottom=0, padleft=0, padright=0):
    ali = gtk.Alignment(xalign=xalign, yalign=yalign)
    ali.add(widget)
    ali.set_padding(padtop, padbottom, padleft, padright)
    return ali

The result : result Thanks in advance.


Solution

  • You didn't specify the Gtk or the python version, so it's difficult to answer. This code is for Gtk 3, and Python 3, using introspection. Maybe this helps:

    hbox = Gtk.HBox()
    large_label = Gtk.Label("Large label")
    self.ajuste = Gtk.Adjustment(0.0, 0.0, 101.0, 0.1, 1.0, 10)
    self.vscale = Gtk.VScale.new(self.ajuste)
    
    vbox = Gtk.VBox()
    lbl = Gtk.Image.new_from_icon_name( "system-run", Gtk.IconSize.DND)
    vbox.pack_start(lbl, False, False, 0)
    vbox.pack_start(self.vscale, True, True, 0)
    
    hbox.pack_start(large_label, False, False, 0)
    hbox.pack_start(vbox, False, False, 0)
    
    self.add(hbox)
    self.show_all()
    

    Note that in vbox.pack_start for the vscale, the sizing options are True, so that the scale can expand.

    You can also call set_size_request on the scale, and you can change the margins with set_margin_left, set_margin_right, _bottom and _top. The result of the above code is:

    enter image description here