473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 13565
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.