471,348 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

GTK + Glade + Python combo box

2
Hello everyone, I'm new to this forum and a sort of beginner at Python. I've created a GUI in Python using Glade and GTK and I have two questions. I'm having trouble with comboboxes. Particularly with entering data into a combo box from Python. I've tried the following....

Expand|Select|Wrap|Line Numbers
  1. myCombo = self.wTree.get_widget('comboboxentry')
  2. myCombo.append_text('blah blah blah')
  3.  
to no avail... apparently you have to first do gtk.combo_box_new_text() before you can append text. Is this possible when using Glade to construct my GUI?

My second question is this.. How do I create a ListBox using Glade or GTK. I would like eventually to be able to grab text from a line via mouse click. For example, a list of files appear in a text area and the user would be able to double click on the file and be able to delete it etc... Any help would be greatly appreciated. Thank you.
Jun 11 '07 #1
4 13406
bartonc
6,596 Expert 4TB
I'm not sure this forum actually has any GTK experts.
The closest that I have come to using tools such as yours was way back when I first started using wxPython. My first GUI generator was wxGlade, but I understand that the two projects are not related (code-wise).

Sorry that I couldn't be of more assistance.
Jun 11 '07 #2
francp
2
You could use a treeview.
Use Glade to create a window with a treeview and a button.
Add a destroy signal for the window and a clicked signal for the button.
Save as test.glade.
Here is a python example that you can run from a terminal:
Expand|Select|Wrap|Line Numbers
  1.  
  2. import pygtk
  3. pygtk.require("2.0")
  4. import gtk, gtk.glade
  5.  
  6. data=[["USD","American Dollar",0.748223],
  7.     ["GBP","British Pound",1.47886],
  8.     ["CAD","Canadian Dollar",0.700714],    
  9.     ["EUR","Euro",1.0],
  10.     ["AUD","Australian Dollar",0.628505]]
  11.  
  12.  
  13. class test:
  14.     def __init__(self):
  15.  
  16. #get the  widget tree from the Glade file
  17.         self.tree=gtk.glade.XML ("test.glade","window1")
  18.  
  19. #connect the signals to the callbacks
  20.         self.tree.signal_autoconnect (self)
  21.  
  22. #load the data into a ListStore
  23.  
  24.         store=gtk.ListStore(str,str,float)
  25.         for row in data:
  26.             store.append(row)
  27.  
  28. #find the treeview widget and link to the store
  29.         self.view=self.tree.get_widget("treeview1")
  30.         self.view.set_model(store)
  31.  
  32. #create the columns for the treeview
  33.         render=gtk.CellRendererText()
  34.         col=gtk.TreeViewColumn("Code",render,text=0)
  35.         self.view.append_column(col)
  36.         render=gtk.CellRendererText()
  37.         col=gtk.TreeViewColumn("Name",render,text=1)
  38.         self.view.append_column(col)
  39.         render=gtk.CellRendererText()
  40.         col=gtk.TreeViewColumn("Rate",render,text=2)
  41.         self.view.append_column(col)
  42.  
  43. #set the selection option so that only one row can be selected
  44.         sel=self.view.get_selection()
  45.         sel.set_mode(gtk.SELECTION_SINGLE)
  46.  
  47. #show the treeview
  48.         self.view.show()
  49.  
  50.  
  51.  
  52.  
  53. #here are the callbacks
  54.  
  55.  
  56.     def on_window1_destroy(self,w):
  57.         print "window1_destroy"
  58.         gtk.main_quit()
  59.  
  60.     def on_button1_clicked(self,w):
  61.         print "button1_clicked"
  62. #find which row was selected (if any)
  63.         sel=self.view.get_selection()
  64.         (model,iter)=sel.get_selected()
  65.  
  66. #print result
  67.         if iter == None:
  68.             print "no row selected"
  69.         else :
  70.             print list(model[iter])
  71. #finish
  72.         gtk.main_quit()
  73.  
  74.  
  75. test()
  76. gtk.main()
  77.  
Jun 16 '07 #3
francp
2
Here is a combobox version
You can put a list of strings in the combobox during the Glade session, or you can replace this with your own list of strings in the program
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2.  
  3. import pygtk
  4. pygtk.require('2.0')
  5. import gtk, gtk.glade
  6.  
  7. data=["harry","pat","june","sarah","william"]
  8.  
  9. class cbtest:
  10.     def __init__(self):
  11.  
  12. #get the  widget tree from the Glade file
  13.     self.tree=gtk.glade.XML ("cbtest.glade","window1")
  14.  
  15. #connect the signals to the callbacks
  16.     self.tree.signal_autoconnect (self)
  17.  
  18. #find the combobox widget in the widget tree
  19.     self.box=self.tree.get_widget("combobox1")
  20.  
  21.  
  22. #if you set up the string list in Glade, than you can miss this next bit.
  23. #but if you want to set up the string list from the program: 
  24.     store=gtk.ListStore(str)
  25.     store.append(["Choose a name"])
  26.     for d in data:
  27.         store.append([d])   #note: append a list
  28.     self.box.set_model(store)   #this replaces the model set by Glade
  29.  
  30. #make the first row active
  31.     self.box.set_active(0)
  32.  
  33. # you must have set a changed signal for combobox1 in cbtest.glade
  34.     def on_combobox1_changed(self, box):
  35.         model = box.get_model()
  36.         index = box.get_active()
  37.         if index:
  38.             print  model[index][0], 'selected'
  39.  
  40.     def on_window1_destroy(self,w):
  41.         gtk.main_quit()
  42.  
  43.  
  44. if __name__ == "__main__":
  45.     cbtest()
  46.     gtk.main()
  47.  
Jun 17 '07 #4
bartonc
6,596 Expert 4TB
francp, you are awesome! Thank you so much for posting in the Python forum.

This is the first chance that I have gotten to see what the GTK interface looks like in practice.

I hope to see more Q&A on the subject in the future.

Thanks, again, for joining the Python Forum on TheScripts.com,
Barton
Jun 17 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Hans Deragon | last post: by
1 post views Thread by Will Ware | last post: by
9 posts views Thread by P. Jouin | last post: by
5 posts views Thread by somesh | last post: by
6 posts views Thread by Doug | last post: by
reply views Thread by Terry Hancock | last post: by
5 posts views Thread by Kveldulv | last post: by
2 posts views Thread by Greg Johnston | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.