[pygtk] Doc/tutorial on creating custom widgets

Stephen Fairchild signupaddress at bethere.co.uk
Fri Apr 4 18:40:23 WST 2008


On Thursday 03 April 2008 18:58:03 Mitko Haralanov wrote:
> I am trying to create a custom widget that is basically a GtkButton but
> along with the label it also has a checkbutton embedded into the button
> face.

Checkbuttons or any other widget can be embedded into a button face as this 
small piece of code demonstrates. Passing on the button clicks is a bit more 
involved but in this simple example is not necessary.

#!/usr/bin/python

import pygtk
pygtk.require("2.0")
import gtk

class ButtonCheckButton(gtk.Button):
   def get_active(self):
      return self.checkbutton.get_active()
   def set_active(self, active):
      self.checkbutton.set_active(active)
   def cb_clicked(self, widget):
      self.checkbutton.set_active(not self.checkbutton.get_active())
   def __init__(self, checklabel = None):
      gtk.Button.__init__(self)
      self.checkbutton = gtk.CheckButton(checklabel)
      self.add(self.checkbutton)
      self.connect("clicked", self.cb_clicked)

if __name__ == "__main__":
   window = gtk.Window(gtk.WINDOW_TOPLEVEL)
   window.set_border_width(10)
   bcb = ButtonCheckButton("Checktext")
   window.add(bcb)
   window.connect("delete-event", gtk.main_quit)
   window.show_all()
   gtk.main()
-- 
Stephen Fairchild.


More information about the pygtk mailing list