#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    438
    Rep Power
    67
    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 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep 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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    438
    Rep Power
    67
    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.)
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)

IMN logo majestic logo threadwatch logo seochat tools logo