source: Replace Widget
import pygtk
pygtk.require("2.0")
import gtk
NAME="Replace Widget"
class t_replace_widget(gtk.Window):
def __init__(self):
gtk.Window.__init__(self)
self.set_title("Replace Widget")
self.set_geometry_hints(min_width=300, min_height=200)
self.connect("destroy", gtk.main_quit)
self.current_widget = None
self.cpt = 0
s = gtk.ScrolledWindow()
s.show()
self.add(s)
self.box = gtk.VBox(2)
self.box.set_homogeneous(False)
self.box.set_spacing(5)
self.box.show()
s.add_with_viewport(self.box)
box2 = gtk.HBox(2)
b = gtk.Button("set a label widget")
b.connect("clicked", self.on_button1_clicked)
b.show()
box2.pack_start(b)
b = gtk.Button("set a button")
b.connect("clicked", self.on_button2_clicked)
b.show()
box2.pack_end(b)
box2.show()
self.box.pack_end(box2, expand=False, fill=False)
def on_button1_clicked(self, widget):
l = gtk.Label("Label: %s" % self.cpt)
self.cpt += 1
l.show()
if self.current_widget is not None:
self.box.remove(self.current_widget)
self.current_widget = l
self.box.pack_start(l)
print "button1 clicked"
def on_button2_clicked(self, widget):
print "button2 clicked"
b = gtk.Button("Button: %s" % self.cpt)
b.show()
self.cpt += 1
if self.current_widget is not None:
self.box.remove(self.current_widget)
self.current_widget = b
self.box.pack_start(b)
t = t_replace_widget()
t.show()
gtk.main()