When making a custom widget in pygtk, what class should it inherit from? I want to be able to put the widget inside other widgets, but I don't want other people to put开发者_运维百科 stuff in mine. Usually I make my widgets inherit from gtk.HBox
or gtk.VBox
, and that works fine, but it is possible then for someone to do a pack_start()
on my widget and cause strange things to happen. I'd inherit from gtk.Widget
but then how do I add things to it? I'd inherit from gtk.Container
or gtk.Bin
but the docs say they are abstract classes.
If your custom widget contains other (probably standard) widgets, you could simply raise an exception in the overridden pack_
methods. That way, nobody can put stuff in it (easily). Inside your class, you then have to use super(...).pack_xxx
instead of self.pack_xxx
.
But it's probably better to derive from gtk.Container
. Then you'll have to implement its abstract methods like do_add(self, widget)
.
In case you only draw custom content (no children), there's no need to derive from a container widget. See the tutorial on pygtk.org.
精彩评论