This document provides instructions on how to create chained (interdependent) combobox selections using Glade and Ruby. It explains how to create a GUI with three comboboxes using Glade where the options in the second and third combobox depend on the selection in the previous combobox. It then provides the Ruby code to populate the comboboxes with data from a database using ActiveRecord and handle the changed signals to update the dependent comboboxes.
1. A Exercise on Interdependent select (or chained select) using
ComboBoxEntry in Glade3 And Ruby
Core:
w are going to develop chained selection module using comboboxentry .
In first comboboxentry, will list out countries names, when user select any one of
countries from this list, application will display another dependent comboboxentry.
The second comboboxentry have to list out “states” which are all corresponding to
selected country in first comboboxentry box.
Then it will be displayed , third comboboxentry & it will list out cities corresponding
to the selected state in second comboboxentry box.
How to install ruby :
$sudo apt-get install ruby
How to install glade :
$sudo apt-get install glade
Or goto 'synaptic package mamager' and find glade3, select mark for installation and click
'Apply' button.
ruby-glade-create-template command :
To run this command we have to install ruby package,
$sudo apt-get install libglade2-ruby
How to install active-record :
$sudo apt-get install rubygems
$sudo gem install activerecord
Error :
While installing activerecord , it will show following error:
2. usr/bin/gem:10: undefined method `manage_gems' for Gem:Module (NoMethodError)
For that do the following:
$sudo gedit /usr/bin/gem
add the following two lines in top of the coding :
require 'rubygems'
require 'rubygems/gem_runner'
and comment the next line by
#Gem.manage_gems
Glade Guide
1). Drag and drop “Window” from Top Level Window
2). Place “Fixed Layer” from Container in window
3). Take three “ComboBoxEntry” and place in window.
4). Set lable name to each comboboxentry and comboboxentry-entry1 to identify.
Signal:
set “changed “ signal to all comboboxentry widget. And save it as
“combo.glade”
Open Glade3 from Programming Application
7. Drag and drop the 'Label' widget & change label name & allign it.
Final design view
8. Ruby Guide
$ ruby-glade-create-template combo.glade > combo.rb
Initialize Method :
Add one line inside initialize method as follow .
@glade[“Window1”].show_all
“Window1” is a default lable name of top level window. If u want change that lable
name...
Program :
# Active Record
require 'libglade2'
require 'rubygems'
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection(
:adapter => quot;mysqlquot;,
:encoding => quot;utf8quot;,
:database => quot;countryquot;,
:username => quot;rootquot;,
:password =>quot;passwordquot;,
:socket => quot;/var/run/mysqld/mysqld.sockquot;,
:dbfile => quot;:memory:quot;
)
ActiveRecord::Schema.define do
create_table :countries do |table|
table.column :name, :string
end
9. create_table :states do |table|
table.column :country_id, :integer
table.column :name, :string
end
create_table :cities do |table|
table.column :state_id, :integer
table.column :name, :string
end
end
=begin
create database “ country” in your mysql by excuting this query
“mysql > create database country; “.
And set your mysql root user name “root” and “password” in the above code.
You can understand easily by seeing the above code, how active record creates tables and
corresponding fields.
Create_table: 'table name' - - - > It will create table in db.
Column: name, :String ( Field Type) - - -> It will set column field by this type and name.
=end
# Relationships :
class Country < ActiveRecord::Base
has_many :state
end
class State < ActiveRecord::Base
belongs_to :country
has_many :cities
end
class City < ActiveRecord::Base
belongs_to :state
end
=begin
This tells about relationships among those tables...
10. “Country “ table record entries having relationship with so many “State” table record
entries...
simply we can represent by one to many relationship, so in this case we have used “
has_many ”. This means a country “has_many” states.
“State” table record entries having relations under “Country” table record entries.... It can
be represented by “ belongs_to “. Since every state belongs to a country hence we have used
“belongs_to” in the state class to say that it belongs to a country.
=end
# Populating values into tables :
country=Country.new(:name=>'India')
country.save
# here we are inserting value 'India' into country table
country.state << State.new(:name => 'TamilNadu')
# it will insert value 'TamilNadu' into state table with country_id
country.state.find(:all,:conditions => {:name => 'TamilNadu'}).first.cities << City.new(:name
=>'Chennai')
# it will append the new entries ' Chennai ' in “city” table with 'Country_id' and ' State_id' .
country=Country.new(:name=>'America')
country.save
country.state << State.new(:name => 'Ohio')
country.state.find(:all,:conditions => {:name => 'Ohio'}).first.cities << City.new(:name
=>'Chicago')
# Like wise, you can insert values into three tables as your wish
# and insert more country, states and city names in corresponding tables . . .
class ComboCountryGlade
include GetText
attr :glade
def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
bindtextdomain(domain, localedir, nil, quot;UTF-8quot;)
@glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler|
method(handler)}
11. @glade['window1'].show_all
country_combo _method
end
# ComboBoxEntry Methods
# country method :
def country_combo_method
@glade[quot;country_comboboxentryquot;].show_all
@glade[quot;state_comboboxentryquot;].hide_all
@glade[quot;State_labelquot;].hide_all
@glade[quot;city_comboboxentryquot;].hide_all
@glade[quot;City_labelquot;].hide_all
# here, we are hiding state and city comboboxentry and their lables
@country_combobox = @glade[quot;country_comboboxentryquot;]
@country_list = Gtk::ListStore.new(String)
@country_cell = Gtk::CellRendererText.new()
# here, we are setting comboboxentry properties to box by calling inbuild Gtk methods
@country_combobox.pack_start(@country_cell, true)
@country_combobox.add_attribute(@country_cell, 'text',0)
@country_combobox.set_model(@country_list)
@country=Country.all
# here, we calling active record class.
# [Country.all] ---> it will return all countries entries
@country.each do |country_name|
@country_iter =@country_list.append
@country_iter[0] =country_name.name
end
end
=begin
here, c_name is iterative variable of @country array record.
From that, we need “ country name field alone”, so that we use [c_name.name] ----> it will
return countries name.
[@c_list_cat.append] -----> it will helps to append strings in comboboxentry.
=end
12. # ComboBoxEntry change signal code : for country
def on_country_comboboxentry_changed(widget)
@country_click_iter= @glade['country_comboboxentry'].active_iter
@country_list.each do |model,path,@iter_country|
if path.to_s == @country_click_iter.to_s
@glade['country_comboboxentry-entry1'].text =@iter_country[0]
country_name=@glade['country_comboboxentry-entry1'].text
@state_combo = @glade[quot;state_comboboxentryquot;]
@state_combo.clear
state_combo_method(country_name)
end
end
end
=begin
active_iter : return , pointer of comboboxentry list by corresponding to mouse or keyboard
press on list.
path.to_s : to identify TreeView path of combobox entry.
state_combo(country_name) : Iside country change signal method, we are going to call “
State Comboboxentry and passing argument as country name, which is selected by user at
run time.
same like this logic we have to write code for state and city comboboxentries also .
=end
# State mathod :
def state_combo_method(country_name)
@glade[quot;state_comboboxentryquot;].show_all
@glade[quot;State_labelquot;].show_all
@glade[quot;city_comboboxentryquot;].hide_all
@glade[quot;City_labelquot;].hide_all
@state_combobox = @glade[quot;state_comboboxentryquot;]
@state_list = Gtk::ListStore.new(String)
@state_cell = Gtk::CellRendererText.new()
@glade['state_comboboxentry-entry2'].text=quot;quot;
13. @state_combobox.pack_start(@state_cell, true)
@state_combobox.add_attribute(@state_cell, 'text',0)
@state_combobox.set_model(@state_list)
country1=Country.find(:all,:conditions=>{:name=>country_name}).first.state
country1.each do |state_name|
@state_iter =@state_list.append
@state_iter[0] =state_name.name
end
end
# ComboBoxEntry change signal code : for state
def on_state_comboboxentry_changed(widget)
state_click_iter= @glade['state_comboboxentry'].active_iter
@state_list.each do |model,path,@iter_state|
if path.to_s == state_click_iter.to_s
@glade['state_comboboxentry-entry2'].text =@iter_state[0]
state_name= @glade['state_comboboxentry-entry2'].text
@city_combo=@glade[quot;city_comboboxentryquot;]
@city_combo.clear
city_combo_method( state_name)
end
end
end
end
# city method :
def city_combo_method(state_name)
@glade[quot;city_comboboxentryquot;].show_all
@glade[quot;City_labelquot;].show_all
@city_combo = @glade[quot;city_comboboxentryquot;]
@city_list = Gtk::ListStore.new(String)
@city_cell = Gtk::CellRendererText.new()
@glade['city_comboboxentry-entry3'].text=quot;quot;
@city_combo.pack_start(@city_cell, true)
14. @city_combo.add_attribute(@city_cell, 'text',0)
@city_combo.set_model(@city_list)
city=State.all(:conditions=>{:name=>state_name}).first.cities
city.each do |city_name|
@city_iter =@city_list.append
@city_iter[0] =city_name.name
end
end
# ComboBoxEntry change signal code : for city
def on_city_comboboxentry_changed(widget)
@city_click_iter= @glade['city_comboboxentry'].active_iter
@city_list.each do |model,path,@iter_city|
if path.to_s == @city_click_iter.to_s
@glade['city_comboboxentry-entry3'].text =@iter_city[0]
end
end
end
# Main program for glade
if __FILE__ == $0
# Set values as your own application.
PROG_PATH = quot;combo_country.gladequot;
PROG_NAME = quot;YOUR_APPLICATION_NAMEquot;
ComboCountryGlade.new(PROG_PATH, nil, PROG_NAME)
Gtk.main
end
# End of program
=begin
Save the whole program as combo.rb
And then run this program in terminal.
$ruby combo.rb
while running this combo.rb , combo.glade file must contain in the same folder.
=end
17. Output window : 7
Use :
Using this Chained Selection program , we are developing a school management application
with following scenerio .
first select one 'class' from class_combobox_entry and then choose 'section' from
section_combobox_entry and then finally select 'student name' from
student_combobox_entry.
You can download this source code from : http://gist.github.com/127848
http://comboboxentry-glade-ruby.blogspot.com/