Skip to content

Instantly share code, notes, and snippets.

@nzjrs nzjrs/gtkmodebutton.py
Last active Dec 11, 2015

Embed
What would you like to do?
A PyGObject / GTK implementation of a ModeButton / ModeButtonBox
from gi.repository import Gtk
class _ButtonBoxMixin(object):
def add(self, widget):
self.pack_start(widget)
def pack_start(self, widget, *args, **kwargs):
if type(widget) is Gtk.RadioButton:
widget.props.draw_indicator = False
flags = Gtk.JunctionSides.NONE
if self._n > 0:
widget.get_style_context().set_junction_sides(Gtk.JunctionSides.LEFT)
if self._last is not None:
ctx = self._last.get_style_context()
ctx.set_junction_sides(
ctx.get_junction_sides() | Gtk.JunctionSides.LEFT)
Gtk.Box.pack_start(self, widget, False, False, 0)
self._last = widget
@property
def group(self):
return self._last
def add_radiobutton(self, label, stock_id="", image=None):
b = Gtk.RadioButton.new_with_label_from_widget(self.group, label)
if stock_id:
image = Gtk.Image.new_from_stock(stock_id, Gtk.IconSize.BUTTON)
if image:
b.set_image(image)
self.pack_start(b)
return b
class GtkModeButtonBox(_ButtonBoxMixin, Gtk.ButtonBox):
def __init__(self, *args, **kwargs):
Gtk.ButtonBox.__init__(self, *args, **kwargs)
self.set_layout(Gtk.ButtonBoxStyle.CENTER)
self.get_style_context().add_class(Gtk.STYLE_CLASS_LINKED)
self._n = 0
self._last = None
def set_min_child_width(self, w):
data = "* { -GtkButtonBox-child-min-width : %d; }" % w
provider = Gtk.CssProvider()
provider.load_from_data(data)
# 600 = GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
self.get_style_context().add_provider(provider, 600)
class GtkModeBox(_ButtonBoxMixin, Gtk.Box):
def __init__(self, *args, **kwargs):
Gtk.Box.__init__(self, *args, **kwargs)
self.get_style_context().add_class(Gtk.STYLE_CLASS_LINKED)
self._n = 0
self._last = None
if __name__ == "__main__":
import random
w = Gtk.Window()
w.set_default_size(500,-1)
w.connect("delete-event", Gtk.main_quit)
vb = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
box = GtkModeButtonBox(orientation=Gtk.Orientation.HORIZONTAL)
for i in range(4):
txt = "--- %d %s" % (i,"-"*random.randrange(1,7))
btn = box.add_radiobutton(txt, Gtk.STOCK_YES)
box.add(Gtk.Button("Normal"))
box.add(Gtk.Button.new_from_stock(Gtk.STOCK_HOME))
vb.add(box)
box = GtkModeBox(orientation=Gtk.Orientation.HORIZONTAL)
box.add(Gtk.Button.new_from_stock(Gtk.STOCK_GO_BACK))
box.add(Gtk.Button.new_from_stock(Gtk.STOCK_GO_FORWARD))
box.props.halign = Gtk.Align.CENTER
vb.add(box)
w.add(vb)
w.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.