Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from gi.repository import Gtk, cairo
- class OwnContainer(Gtk.Container):
- def __init__(self):
- Gtk.Container.__init__(self)
- self.set_has_window(False)
- self.child_widget = None
- # own method to set child ...
- def set_child(self, child):
- if self.child_widget is not None:
- return
- self.child_widget = child
- self.child_widget.set_parent(self)
- # ... and default one
- def do_add(self, child):
- self.set_child(child)
- def do_remove(self, child):
- child.unparent()
- self.child_widget = None
- def do_get_request_mode(self):
- return Gtk.SizeRequestMode.WIDTH_FOR_HEIGHT
- # all 'preferred' methods are actually stubs and return fixed values
- # container should calculate that values according to its content
- def do_get_preferred_width(self):
- return (200, 200)
- def do_get_preferred_height(self):
- return (260,260)
- def do_get_preferred_width_for_height(self, height):
- return (200, 200)
- def do_get_preferred_height_for_width(self, width):
- return (260, 260)
- # propagate action to childs
- # Some bug occur here on window destroy, see http://stackoverflow.com/questions/21214732/custom-gtk-container-with-pygobject
- def do_forall(self , include_internals , callback):
- if self.child_widget is not None:
- callback(self.child_widget)
- # that method decides where to put child
- # implement it as you wish
- def do_size_allocate(self, allocation):
- self.set_allocation(allocation)
- if self.child_widget is not None:
- if self.child_widget.get_visible():
- print('showed')
- button_alloc = cairo.RectangleInt()
- button_alloc.x = allocation.x + 60
- button_alloc.y = allocation.y + 60
- button_alloc.width = 100
- button_alloc.height = 140
- self.child_widget.size_allocate(button_alloc)
- def main():
- win = Gtk.Window()
- win.connect('destroy', Gtk.main_quit)
- container = OwnContainer()
- button = Gtk.Button('Hello')
- container.set_child(button)
- win.add(container)
- win.show_all()
- Gtk.main()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement