The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
List assignment in ComboBox not working
Discuss List assignment in ComboBox not working in the Python Programming forum on Dev Shed. List assignment in ComboBox not working Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 19th, 2012, 01:47 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 1 h 32 m 54 sec
Reputation Power: 0
|
|
|
List assignment in ComboBox not working
Hello,
I am trying to create a combobox and set values using output of a query executed in Oracle database.
On assigning the list in combobox.SetItems(list), I get the following error..
TypeError: String or Unicode type required
However, if I cast the input as str ie., combobox.SetItems(str(list)), the output I see in the combobox is wierd.
If the output of the query is
5
4
The combobox displays
[
5
,
4
]
How to assign a list to the combobox ?
Code:
import wx
import cx_Oracle
class PerfFrame(wx.Frame):
def __init__(self,parent,title):
frame = wx.Frame.__init__(self,parent,title=title, size=(500, 500))
self.panel = wx.Panel(frame)
lbl_InstanceID = wx.StaticText(self, -1, "Values List")
self.combobox1 = wx.ComboBox(self, 1, style=wx.CB_DROPDOWN, pos=wx.Point(100, 30), choices=[])
self.combobox1.SetSelection(0)
lst=[]
new_list=[]
lst=self.populate_instanceid()
for i in lst:
new_list.append(i)
self.combobox1.SetItems(new_list)
self.Show()
def populate_instanceid(self):
con=cx_Oracle.connect ("Connection String")
curs=con.cursor()
curs.execute("select to_number(inst_id) from gv$instance")
inst_list=[]
for row in curs.fetchall()[0]:
inst_list.append(row)
return inst_list
app=wx.App(False)
frame=PerfFrame(None,"Perf")
app.SetTopWindow(frame)
app.MainLoop()
Rgds,
Gokul
|

October 19th, 2012, 06:11 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by gokulkumargopal How to assign a list to the combobox ? |
SetItems() method is the way, so I think you need to check what the populate_instance() method is returning. Simply insert debugging lines like “print(inst_list)”, “print(type(inst_list))”, “print([item for item in inst_list])” as necessary and see what you got in the console.
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

October 19th, 2012, 12:56 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 1 h 32 m 54 sec
Reputation Power: 0
|
|
|
Solved
Hello,
Thanks for your response.
It appears the values in the choices list must be a set of strings and not integers.
The following code errors..
Code:
self.combobox1 = wx.ComboBox(self, 1, style=wx.CB_DROPDOWN, pos=wx.Point(100, 30), choices=[1,2,3,4,5])
This succeeds..
Code:
self.combobox1 = wx.ComboBox(self, 1, style=wx.CB_DROPDOWN, pos=wx.Point(100, 30), choices=['1','2','3','4','5'])
Rgds,
Gokul
|

October 19th, 2012, 01:50 PM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by gokulkumargopal It appears the values in the choices list must be a set of strings and not integers. |
Yes, of course. It’s easy enough to fix, just use list comprehension like:
Code:
choices = [str(choice) for choice in choices]
(using the name you gave in your last example.)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|