I’m struggling with a ComboRow. I want it to display three options; two Labels and a Gtk.SpinButton with an adjacent Label. Ideally, if the user chooses the SpinButton I’d like it to only display the Label (the SpinButton in the ComboRow isn’t functional and ugly) but first I need to get the ComboRow to behave better!
It would be easier to show the problem with screenshots but I can’t seem take one when the ComboRow is showing it’s options (could that be Gnome bug?)
I’ve tried lots of ways of doing this and all work but not without problems. Below is the closest I’ve got.
The two Labels and SpinButton (spin_box) are available as an option, a value can be set on the SpinButton and when the SpinButton is chosen it shows as I’d expect in the ComboRow. But if the ComboRow options are presented again the SpinButton isn’t visible as an option. You can still choose it (the Box, I guess) and the chosen value from earlier is still set but no SpinButton.
class BackupObject(GObject.Object):
__gtype_name__ = 'BackupObject'
def __init__(self, string=None, spinbutton=None):
GObject.Object.__init__(self)
self._string = string
self._spinbutton = spinbutton
@GObject.Property(type=str)
def string(self):
return self._string
@GObject.Property(type=object)
def spinbutton(self):
return self._spinbutton
def backup_factory_setup(factory, list_item):
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
spin_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
label = Gtk.Label()
spin_label = Gtk.Label(label=" days")
box.append(label)
box.append(spin_box)
box.append(spin_label)
list_item.set_child(box)
def backup_factory_bind(factory, list_item):
box = list_item.get_child()
label = box.get_first_child()
spin_box = label.get_next_sibling()
spin_label = spin_box.get_next_sibling()
option = list_item.get_item()
if option.string:
label.set_text(option.string)
label.set_visible(True)
spin_box.set_visible(False)
spin_label.set_visible(False)
else:
if option.spinbutton.get_parent():
option.spinbutton.get_parent().remove(option.spinbutton)
spin_box.append(option.spinbutton)
spin_box.set_visible(True)
spin_label.set_visible(True)
label.set_visible(False)
self.backup_factory.connect("setup", backup_factory_setup)
self.backup_factory.connect("bind", backup_factory_bind)
self.backup_liststore.append(BackupObject(string="Forever"))
self.backup_liststore.append(BackupObject(string="Never"))
self.backup_liststore.append(BackupObject(spinbutton=Gtk.SpinButton.new_with_range(0, 999, 1)))
If … is commented out the SpinButton remains displayed an an option but doesn’t show on the ComboRow as selected.
if option.spinbutton.get_parent():
option.spinbutton.get_parent().remove(option.spinbutton)
I also get the error…
Gtk-CRITICAL **: 15:15:50.344: gtk_box_append: assertion 'gtk_widget_get_parent (child) == NULL' failed
Thank you for any help!