Open Technical Blog
Wednesday, 25 May 2016
ComboBox 01
The script shows how to create a combo box in a GTK3 window.
Define a text combo box without entry.
Define a text combo box with entry.
Define event handlers for the combo boxes.
Source Code
#!/usr/bin/python import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk class Base: def __init__(self): self.win = Gtk.Window() self.win.set_title("Combos") self.win.set_default_geometry(width=300, height=450) self.win.set_border_width(4) self.win.connect("delete-event", self.WinDelete_event) # # vbox holding all controls mBox = Gtk.VBox(4) mBox.set_homogeneous(False) self.win.add(mBox) # # label mlab01 = Gtk.Label("Gtk.ComboBoxText") mlab01.set_properties(xalign=0.0) mBox.pack_start(mlab01,False,False,2) # combo01 - comboboxtext mcomb01 = Gtk.ComboBoxText() # add data mItems = ("Mango", "Apple", "Banana", "Pineapple", "Melon", "Papaya") for mi in mItems: mcomb01.append_text(mi) mcomb01.set_active(0) # select the 1st item mcomb01.connect("changed", self.comb01_changed) mBox.pack_start(mcomb01,False,False,2) # # label mlab02 = Gtk.Label("Gtk.ComboBoxText with entry") mlab02.set_properties(xalign=0.0) mBox.pack_start(mlab02,False,False,2) # combo02 - comboboxtext with entry mcomb02 = Gtk.ComboBoxText().new_with_entry() # add data mItems = ("FreeBSD", "Windows 10", "Debian Linux", "Mac OSX", "Solaris", "Fedora Linux") for mi in mItems: mcomb02.append_text(mi) mcomb02.set_active(0) # select the 1st item mcomb02.connect("changed", self.comb02_changed) mBox.pack_start(mcomb02,False,False,2) # # status bar self.stsBar = Gtk.Statusbar() mBox.pack_start(self.stsBar,False,True,2) # self.win.show_all() # Event Handlers # --------------------------------------------------------- # Delete event def WinDelete_event(self, event, data=None): Gtk.main_quit() return False # combo01 changed def comb01_changed(self, combo): mName = combo.get_active_text() # get iter if mName <> None: mStr = 'C1: ' + mName self.pushStatus("c1",mStr) # combo02 changed def comb02_changed(self, combo): mName = combo.get_active_text() # get iter if mName <> None: mStr = 'C2: ' + mName self.pushStatus("c2",mStr) # push message to status bar def pushStatus(self, pCtxt, pMsg): mctxid = self.stsBar.get_context_id(pCtxt) self.stsBar.push(mctxid, pMsg) def main(): Gtk.main() if __name__ == "__main__": base = Base() main()
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment