How to insert into listbox using wxPython

Discussion in 'Python' started by Brian Kelley, Jan 8, 2004.

  1. Brian Kelley

    Brian Kelley Guest

    Andrew wrote:

    > Hi I just started learning wxPython
    >
    > I wanted to know how I could do this in wxPython
    >
    >
    > self.listbox.delete(0, END)
    > for item in self.results:
    > self.listbox.insert(END, item)
    >
    >
    > I don't know but I think the insert and delete things here are specific of
    > Tkinter which I have been studying for the last little while
    >


    # make a list box with default choices
    box = wxListBox(self, -1, choices=['a','b','c'])
    # append a new entry
    box.Append('d')
    # reset the listbox to something else
    box.Set(['1','2','3'])

    # delete the second entry selection
    box.Delete(1)

    # get the index of currently selected entry
    print box.GetSelection()

    # get the string that is selected
    print box.GetStringSelection()

    # make a callback for when an entry is selected
    def listboxhandler(evt):
    box = evt.GetEventObject()
    print box.GetSelection(), "selected index"
    print box.GetStringSelection(), "selected item"

    EVT_LISTBOX(box, box.GetId(), listboxhandler)

    full little program
    ###########################################################
    from wxPython.wx import *

    class T(wxFrame):
    def __init__(self, parent, id, title):
    print "calling init"
    wxFrame.__init__(self, parent, id, title)
    self.mainsizer = self.sizer = wxBoxSizer(wxVERTICAL)
    # make a list box with default choices
    box = wxListBox(self, -1, choices=['a','b','c'])
    # append a new entry
    box.Append('d')
    # reset the listbox to something else
    box.Set(['1','2','3'])
    box.Delete(0)
    print box.GetSelections()
    self.sizer.Add(box, 0, wxEXPAND)
    self.SetSizer(self.sizer)
    def listboxhandler(evt):
    box = evt.GetEventObject()
    print box.GetSelection(), "selected index"
    print box.GetStringSelection(), "selected item"

    EVT_LISTBOX(box, box.GetId(), listboxhandler)

    app = wxPySimpleApp()
    foo = T(None, -1, "hello")
    print foo
    foo.Show()
    app.MainLoop()


    > Anyhelp would be cool
    >
    >
     
    Brian Kelley, Jan 8, 2004
    #1
    1. Advertising

  2. Brian Kelley

    Brian Kelley Guest

    Andrew wrote:
    > Hi thanks for your help but I am still having problems basically I am using
    > a button to connect to a Database and I want to display the data from the
    > database into the listbox
    > Here is the code I am using for the button
    >
    > self.c = self.db.cursor()
    > self.c.execute("SELECT * FROM guests;")
    > self.results = self.c.fetchall()
    > # Here is where I don't know what to do I want to be able to get the
    > data from self.results and display it in the listbox
    > self.listbox.Append('')
    > self.listbox.Set([''])


    It looks as though you are putting nothing in the listbox.

    Append('') adds a blank
    and listbox.Set(['']) replaces the listbox with a blank.

    Try:
    for x in self.results:
    self.listbox.Append(x[0])

    Brian
     
    Brian Kelley, Jan 9, 2004
    #2
    1. Advertising

  3. Brian Kelley

    Andrew Guest

    Hi I just started learning wxPython

    I wanted to know how I could do this in wxPython


    self.listbox.delete(0, END)
    for item in self.results:
    self.listbox.insert(END, item)


    I don't know but I think the insert and delete things here are specific of
    Tkinter which I have been studying for the last little while

    Anyhelp would be cool
     
    Andrew, Jan 9, 2004
    #3
  4. Brian Kelley

    Andrew Guest

    Hi thanks for your help but I am still having problems basically I am using
    a button to connect to a Database and I want to display the data from the
    database into the listbox
    Here is the code I am using for the button

    def OnB2Button(self, event):
    self.db = MySQLdb.connect("localhost", "", "", "guestbook")
    global db
    self.c = self.db.cursor()
    self.c.execute("SELECT * FROM guests;")
    self.results = self.c.fetchall()
    # Here is where I don't know what to do I want to be able to get the
    data from self.results and display it in the listbox
    self.listbox.Append('')
    self.listbox.Set([''])
    self.listbox.Delete(0)
    print self.listbox.GetSelections()
    self.sizer.Add(self.listbox, 0, wxEXPAND)
    self.Set.Sizer(self.sizer)

    Thank you again for your help

    Cheers

    Andrew
     
    Andrew, Jan 9, 2004
    #4
  5. Brian Kelley

    Andrew Guest

    Hi I had tried something similiar to that earlier.

    I typed what you wrote

    for x in self.results:
    self.listbox.Append(x[0])

    and I got the same error as what I had tried earlier

    Traceback (most recent call last):
    File "S:\GUI\MYSQL\mysqlgui.py", line 65, in OnB2Button
    self.listbox.Append(x[0])
    File "F:\Python22\Lib\site-packages\wxPython\controls.py", line 78, in
    Append
    val = controlsc.wxControlWithItems_Append(self, *_args, **_kwargs)
    TypeError: String or Unicode type required

    Any help is alway's appreciated
     
    Andrew, Jan 10, 2004
    #5
  6. Brian Kelley

    David Bolen Guest

    "Andrew" <na> writes:

    > I typed what you wrote
    >
    > for x in self.results:
    > self.listbox.Append(x[0])
    >
    > and I got the same error as what I had tried earlier
    >
    > Traceback (most recent call last):
    > File "S:\GUI\MYSQL\mysqlgui.py", line 65, in OnB2Button
    > self.listbox.Append(x[0])
    > File "F:\Python22\Lib\site-packages\wxPython\controls.py", line 78, in
    > Append
    > val = controlsc.wxControlWithItems_Append(self, *_args, **_kwargs)
    > TypeError: String or Unicode type required
    >
    > Any help is alway's appreciated


    Your database query is returning a list of tuples, where each element
    in the tuple is a column from your database that is part of the query
    (or all columns in the table with your query of *). The tuple is not
    a string, which is what the wxListBox understands how to display.

    I expect that if you change the code to:

    self.listbox.Append(str(x[0]))

    you'll get rid of the error, since that will provide a string
    representation of the tuple x[0], but I also expect it won't be
    exactly what you want depending on the database columns, and/or the
    way certain data types automatically turn themselves into strings.

    In the end you'll probably want to process each entry in 'results'
    according to your own desires for display purposes, formatting an
    appropriate string to be put into the ListBox. You may also find that
    using a wxListCtrl in wxLC_REPORT mode fits well since it will make it
    simpler to divide the columns of data (either that or a wxGrid,
    although wxGrid is probably overkill).

    -- David
     
    David Bolen, Jan 12, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    684
  2. Replies:
    1
    Views:
    1,870
    Albert Hopkins
    Dec 6, 2008
  3. susie

    insert listbox value into database table

    susie, Nov 14, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    162
    susie
    Nov 14, 2003
  4. Feege
    Replies:
    0
    Views:
    502
    Feege
    Dec 20, 2005
  5. Sergio del Amo
    Replies:
    4
    Views:
    266
Loading...

Share This Page