Show Hide Content
Adding Page Content from a Glade file
To add one Glade file into another you need to put a container in the main window. A good container to use is a vbox with one
Copy the two glade files into the lib directory. Ignore the .pyc files that Python creates when you run main.py.
First we need to import the two classes in order to do that we create a python file with a class that has its own builder so we avoid name confilicts. We have to import gtk in this file as well. These are similar to others we have created where we get the Builder and add the glade file. Methods for signals contained in the glade file we add go in this file. This makes the code easier to read and follow.
#!/usr/bin/env python
import gtk
class container1:
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file('lib/container1.glade')
self.builder.connect_signals(self)
def on_button1_clicked(self, widget):
print 'e'
def on_combobox1_changed(self, widget):
print 'x'
#!/usr/bin/env python
import gtk
class container2:
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file('lib/container2.glade')
self.builder.connect_signals(self)
def on_button1_clicked(self, widget):
print 'f'
def on_combobox1_changed(self, widget):
print 'z'
We add several things to main.py to add the new containers to tab 2.
-
Import the container1 and container2 class
-
Create and instance of the classes we imported
-
Add both containers to tab 2
-
After the show all we hide one container
#!/usr/bin/env python
try:
import pygtk
pygtk.require('2.0')
except:
pass
try:
import gtk
import gtk.glade
except:
print('GTK not available')
sys.exit(1)
import lib.myclass as mc
import lib.container1 as c1
import lib.container2 as c2
class main:
def __init__(self):
# create an instance of the Builder
self.builder = gtk.Builder()
self.builder.add_from_file('lib/main.glade')
self.builder.connect_signals(self)
#create an instance of container classes
self.container1 = c1.container1()
self.container2 = c2.container2()
# create the cell renderer
self.cell = gtk.CellRendererText()
# create the liststores for our combobox
self.furry_list = gtk.ListStore(int, str)
self.furry_list.append([0, 'None'])
self.furry_list.append([1, 'Rabbit'])
self.furry_list.append([2, 'Cat'])
self.furry_list.append([3, 'Hamster'])
self.slimy_list = gtk.ListStore(int, str)
self.slimy_list.append([0, 'None'])
self.slimy_list.append([1, 'Snake'])
self.slimy_list.append([2, 'Fish'])
self.slimy_list.append([3, 'Slug'])
#populate the default choice into the combobox
self.cb1 = self.builder.get_object('combobox1')
self.cb1.set_model(self.furry_list)
self.cb1.pack_start(self.cell, True)
self.cb1.add_attribute(self.cell, 'text', 1)
self.cb1.set_active(0)
# add the containers to tab 2 in the main glade
self.page2_vbox2 = self.builder.get_object('vbox2')
self.container1_vbox = self.container1.builder.get_object('vbox1')
self.container1_vbox.reparent(self.page2_vbox2)
self.container2_vbox = self.container2.builder.get_object('vbox1')
self.container2_vbox.reparent(self.page2_vbox2)
# setup the main window
self.window = self.builder.get_object("window1")
self.window.set_title("ComboBoxes")
self.window.show_all()
# after a show all we need to hide the second container
self.container2_vbox.hide()
def on_radiobutton1_toggled(self, widget):
if widget.get_active():
self.cb1.clear()
self.cb1.set_model(self.furry_list)
self.cb1.pack_start(self.cell, True)
self.cb1.add_attribute(self.cell, 'text', 1)
self.cb1.set_active(0)
def on_radiobutton2_toggled(self, widget):
if widget.get_active():
self.cb1.clear()
self.cb1.set_model(self.slimy_list)
self.cb1.pack_start(self.cell, True)
self.cb1.add_attribute(self.cell, 'text', 1)
self.cb1.set_active(0)
def on_window1_destroy(self, object, data=None):
print "quit with cancel"
gtk.main_quit()
main = main()
gtk.main()
Now when we run main.py we see on page 2 container 1. When we push the button an e is printed to the terminal.
Last but not least add signal handlers in main.glade to radiobutton3 toggled and radiobutton4 toggled.
Next add the following two methods to main.py after the radiobutton2 method.
def on_radiobutton3_toggled(self, widget):
if widget.get_active():
self.container1_vbox.show()
self.container2_vbox.hide()
def on_radiobutton4_toggled(self, widget):
if widget.get_active():
self.container1_vbox.hide()
self.container2_vbox.show()
Now when you change the radio button on page 1 the hide/show changes which container is visible.