472,330 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

How to insert into listbox using wxPython

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
Jul 18 '05 #1
5 10449
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


Jul 18 '05 #2
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
Jul 18 '05 #3
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
Jul 18 '05 #4
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

Jul 18 '05 #5
"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
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fooooo | last post by:
How do I center each item in the ListBox widget? Also, is it possible to change the color of the selected item? right now it uses the OSes color. I...
2
by: Jeff | last post by:
I need to insert into a table multiple item of a listbox. (item.text and item.value Right now, I'm using an OleDBConnection with Access, a...
3
by: Big E | last post by:
I'm using ASP.Net and SQL Server 2000. I'm trying to insert or update records from a multiselect listbox. How can I get the contents of the listbox...
5
by: Brad Baker | last post by:
I'm trying to write a simple asp.net page which updates some data in a SQL database. At the top of the page I have the following code: <%@ Page...
6
by: shuf | last post by:
Hello, I am running python 2.4.4 with wxpython 2.6 on Fedora 6. I am trying to set the font color of individual elements in a listbox, but I...
6
by: rahulnag22 | last post by:
Hi, Is it possible to have different items in a listbox in different colors? Or is it just one color for all items in a listbox? Thanks Rahul
1
by: Marcpp | last post by:
Hi, I need add to a listbox a list of items extracted from a database. This is that I've do: class tasques(wx.Frame): def __init__(self, *args,...
1
by: Bailu | last post by:
Hi, I am a newbie in wxPython and doing a program with ListBox, I want to select and deselect items in this box, I have use self.devlist =...
3
by: Soren | last post by:
Hi, Id like to make my own special listbox.. I want to able (at the push of a button) to add another item to my special listbox... each item is...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.