472,986 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 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 13523
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

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

Similar topics

3
by: Hans Deragon | last post by:
Greetings. Total newbie to Glade here. I created an interface using glade-2 and I want to use it with my python program. Following is what I wrote (test prg): -------------------------...
1
by: Will Ware | last post by:
In July there was a thread about problems on Red Hat 9 with PyGTK not playing nice with Glade. I was tinkering with this a little bit tonight, starting with the code I found in this article:...
9
by: P. Jouin | last post by:
I work with Linux Mandrake10 and KDE. I have compiled and install : Python 2.3.4 pygtk-2.2.0 Glade2 2.6.0 My problem is : I want to know what can i do for having Python in the Language of...
5
by: somesh | last post by:
hello, I wrote a small tute for my brother to teach him python + glade, plz see, and suggest to make it more professional , In tute I discussed on Glade + Python for developing Applications too...
6
by: Doug | last post by:
Hi all, Can someone tell me why I do not get a connection between the events and the functions in the sample below. GUI window appears OK, just no connections seem to be made. I am new to this...
0
by: Terry Hancock | last post by:
I've run into something a little odd on my Debian-installed Python 2.3. I have deb packages "python2.3", "python-gtk2", and "python2.3-glade2" installed on this machine. Among other things,...
5
by: Kveldulv | last post by:
I made simple GUI in Glade 3 (Ubuntu 7.04) consisting of only 2 buttons. When I run 2buttonsgui.py, no GUI pops out #!/usr/bin/env python import pygtk import gtk.glade class TwoButtonsGUI:...
2
by: Greg Johnston | last post by:
Hey all, I'm a relative newbie to Python (switched over from Scheme fairly recently) but I've been using PyGTK and Glade to create an interface, which is a combo I'm very impressed with. ...
5
by: holmes86 | last post by:
hi,everyone. I am a python newbie.and I write a python program with glade,as following: import sys import gtk import gtk.glade class TLaitSignals: '''Define TLait singals handler'''
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.