473,883 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.de lete(0, END)
for item in self.results:
self.listbox.in sert(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 10629
Andrew wrote:
Hi I just started learning wxPython

I wanted to know how I could do this in wxPython
self.listbox.de lete(0, END)
for item in self.results:
self.listbox.in sert(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.GetSelectio n()

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

# make a callback for when an entry is selected
def listboxhandler( evt):
box = evt.GetEventObj ect()
print box.GetSelectio n(), "selected index"
print box.GetStringSe lection(), "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(wxVE RTICAL)
# 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.GetSelectio ns()
self.sizer.Add( box, 0, wxEXPAND)
self.SetSizer(s elf.sizer)
def listboxhandler( evt):
box = evt.GetEventObj ect()
print box.GetSelectio n(), "selected index"
print box.GetStringSe lection(), "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.Ap pend('')
self.listbox.Se t([''])
self.listbox.De lete(0)
print self.listbox.Ge tSelections()
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.Ap pend('')
self.listbox.Se t([''])


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.Ap pend(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.Ap pend(x[0])

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

Traceback (most recent call last):
File "S:\GUI\MYSQL\m ysqlgui.py", line 65, in OnB2Button
self.listbox.Ap pend(x[0])
File "F:\Python22\Li b\site-packages\wxPyth on\controls.py" , line 78, in
Append
val = controlsc.wxCon trolWithItems_A ppend(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.Ap pend(x[0])

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

Traceback (most recent call last):
File "S:\GUI\MYSQL\m ysqlgui.py", line 65, in OnB2Button
self.listbox.Ap pend(x[0])
File "F:\Python22\Li b\site-packages\wxPyth on\controls.py" , line 78, in
Append
val = controlsc.wxCon trolWithItems_A ppend(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.Ap pend(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
2173
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 would like it to be consistant on every machine.
2
3647
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 Command(Insert string) and parameters. I don't see how an ExecuteNonQuery() would fit in a loop ? Must be a simple solution out there dim Item as listIte for each Item in ListBox1.Item INSERT INTO TABLE... next Item
3
2569
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 if there is more than one selected and update that into the database. But most important how do I update or insert those records. Any articles or ideas would be appreciated. Thanks. Big e
5
2620
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 Language="C#" Debug="true" %> <%@ import namespace="System.Data" %> <%@ import namespace="System.Data.SqlClient" %> <script language="c#" runat="server"> public void Page_Load(object sender, EventArgs e) {
6
6219
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 am not able to with SetItemForegroundColour() (this method does not appear to do anything): #!/usr/bin/env python # -*- coding: UTF-8 -*- # generated by wxGlade 0.4.1 on Thu Mar 22 12:15:29 2007 import wx
6
20080
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
1540
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, **kwds): ..... self.list_box_1_copy = wx.ListBox(self, -1, choices=, style=wx.LB_SINGLE|wx.LB_ALWAYS_SB) ....
1
4609
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 = wx.ListBox(self, style=wx.LB_MULTIPLE) self.Bind(wx.EVT_LISTBOX, self.select_dev, self.devlist) to create this box, but don't know how to implement self.select_dev
3
2319
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 a panel with a label, some buttons and maybe a text control. I've tried adding a new panel object with the stuff i want to the sizer i'm using for my listbox (which is a panel which can contain other panels)... and then run update() and refresh() on everything... But it doesn't work.. i see a...
0
9789
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11129
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10838
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10408
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7967
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.