btn[1]= wxButton(self, -1, MIList[i],(height*22,0),(width,22))
I keep getting erros related to the creation of the button, and I am not
sure how to create an array of buttons.
Thanks
What are the errors? What is btn? What is MIList[i]? We can't help
you unless you are unambiguous and give all the details.
> and I am not
> sure how to create an array of buttons.
The same way you would create a collection of any other object in
Python. Create the object and append it to a list.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
i is currently 0.
btn[i] = wxButton(self, -1, "Hello",(height*22,0),(width,22))
TypeError: object doesn't support item assignment
That is not a problem. Indices start with 0 in Python.
> btn[i] = wxButton(self, -1, "Hello",(height*22,0),(width,22))
> TypeError: object doesn't support item assignment
Could this be your problem?
>>> l = (1,2,3)
>>> l[0] = 1
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> l = [1,2,3]
>>> l[0] = 1
>>>
As you can see, l = (1,2,3) creates a tuple, which is like a list, but
is immutable. To create a list, use [] instead of ().
For future reference, it would help people trying to help you if you
could post more useful information so we don't have to guess. In this
case, at least posting how you created the btn "array" would have
immediately tipped people off to the source of the error. Often errors
aren't obvious by simply looking at the line that generated the
exception (and this seems to be one of those cases).
Regards,
Cliff
--
When I die, don't bury me alone
-These Immortal Souls
Just for interest's sake, what are you trying to do that can only be
done with what you define as an "array" and not with a Python sequence
type?
--
charl p. botha http://cpbotha.net/ http://visualisation.tudelft.nl/
For the most part, this won't matter to many programmers -- who don't
encounter circumstances where serious performance matters. And so they
gain nothing from using an array rather than a sequence. And in general
(in a language like Python) sequences are so much easier and more natural
to use. But in circumstances where you have a very large number of items
that you know you can index directly via integers and where you don't need
to do inserts or deletes to any significant degree, a more efficient data
structure can offer a real gain.
At the moment in my Python apps I use the usual sequences, converting
things to tuples in cases where I know they won't later need to be
changed. But I'm still prototyping. When we go production, a hard look
will be taken at performance (or, if circumstances dictate, even before
that). And at that point it may be necessary to implement some extensions
in C. Greg Ewing, for example, recently told me that he has rewritten Plex
by using Pyrex and achieved a truly substantial performance improvement
(not yet released). For the amount of data I'm dealing with, and the
requirements for interactive use, these things are likely to make a
difference.
To be honest, I've been quite pleasantly surprised about the performance of
Python in general and such things as wxPython and the Python bindings for
BSDDB -- though of course in the latter two cases most of that performance
comes from the underlying C/C++ implentations :-).
--------------------------------------
Gary H. Merrill
Director and Principal Scientist, New Applications
Data Exploration Sciences
GlaxoSmithKline Inc.
(919) 483-8456
What makes you think that Python's list type is implemented as a linked
list? It's not...
(I believe it's a malloc-ed block of memory holding an array of PyObject
pointers, plus a bit of logic to support resizing and the like, but look
at the sources if you care...)
Paul.
b[0] = x
will fail because you are *referencing* b and it is not yet defined.
You need something like:
b = [] # b is an empty sequence
b.append('foo') # append 'foo' to b; this is the moral equivalent of "
b[0] = 'foo' "
You could also use the insert() method, but beware of what Python does when
you insert at a point beyond the current range of a sequence.
So my guess is that you need something like:
btn = [] # an empty sequence we can append to
while whatever:
# Append next button to sequence
btn.append( wxButton(self, -1, "Hello",(height*22,0),(width,22)) )
Think sequences, tuples, lists. Think Lisp. No arrays (something of a
pain in various circumstances, I agree). Python does support arrays via
the class library, but not really to my liking. Someone should do a nice
general and efficient array class that supports arrays of arbitrary
objects.
--------------------------------------
Gary H. Merrill
Director and Principal Scientist, New Applications
Data Exploration Sciences
GlaxoSmithKline Inc.
(919) 483-8456
"Mike Wagman"
<mwa...@charter.net>
To: "wxpython list" <wxPytho...@lists.wxwindows.org>
05-Aug-2003 22:50
Please respond to cc:
wxPytho...@lists.wx Subject: Re: [wxPython-users] Creating Buttons referenced like an array
windows.org
Perhaps my problem is in creation of the first button.
i is currently 0.
btn[i] = wxButton(self, -1, "Hello",(height*22,0),(width,22))
TypeError: object doesn't support item assignment
On Tue, 2003-08-05 at 12:05, Robin Dunn wrote:
> Mike Wagman wrote:
> > Is there a way to create buttons with a command like.
> >
> > btn[1]= wxButton(self, -1, MIList[i],(height*22,0),(width,22))
> >
> > I keep getting erros related to the creation of the button,
>
> What are the errors? What is btn? What is MIList[i]? We can't help
> you unless you are unambiguous and give all the details.
>
> > and I am not
> > sure how to create an array of buttons.
>
> The same way you would create a collection of any other object in
> Python. Create the object and append it to a list.
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-user...@lists.wxwindows.org
For additional commands, e-mail: wxPython-...@lists.wxwindows.org
In your case, I think what you're looking for is a dictionary, not a list.
http://www.python.org/doc/current/lib/typesmapping.html
If you want to use integers as the keys you can, but you can just as easily
use another immutable type like a string. Maybe something like:
>>> buttons = {}
>>> buttons[1] = wx.wxButton(panel, -1, 'Button 1', (0, 30))
>>> buttons[1].GetLabel()
'Button 1'
If you need an "ordered" dictionary, see the Python Cookbook or ask on
comp.lang.python for numerous recipes.
ka
> -----Original Message-----
> From: gary.h....@gsk.com [mailto:gary.h....@gsk.com]
> Sent: Wednesday, August 06, 2003 7:28 AM
> To: wxPytho...@lists.wxwindows.org
> Subject: Re: [wxPython-users] Creating Buttons referenced like an array
>
>
It would be nice if people actually read the postings instead of reading
their own views *into* the postings. First, I never said or suggested that
Python sequences were implemented as *linked* lists. I'm perfectly aware
that they are not. And I never said or suggested that the performance
issues would involve looking up objects in an array of GUI widgets. Was
there *any* mention in my posting or was there *any* example pertaining to
"referencing GUI objects in an array"? No. The performance issues
pertain to the implementation of a variety of fairly complex algorithms and
processes in computational linguistics and text processing (Brill taggers,
Abney parsers, etc.). And they involve very large corpora and very large
ontologies. In these contexts, 100,000 iterations is *nothing*. A single
ontology itself will have over 100,000 terms -- never mind what you want to
*do* with it. And lexicons can easily have half a million entries (not to
mention morphological variants). You can end up having finite state
automata with *millions* of states. And that's just the beginning of the
problem
Have I done any formal studies of Python performance for these things? No.
Do I have some definite knowledge and experience of how these algorithms
behave under a variety of implementations? Yes. Do I have experience with
how Python behaves in implementing similar algorithms? Yes. Do I have any
experience in implementing such things in a variety of contexts and using a
variety of languages? Yes -- among that experience, about 20 years
designing and implementing commercial production compilers, testing them,
and and tuning them against the competition.
You're right that "for most end-user applications, people would be
hard-pressed to differentiate between a C/C++ app and a Python app". And
for those parts of my application for which that is true, I'm perfectly
happy with what the usual Python approach has to offer. But this isn't one
of "most end-user applications". Are there *some* applications in which
performance *really* matters? Yes, indeed there are. I'm afraid you'll
have to excuse me if I'm less than totally relieved by your anecdotes
<wink>.
Someone asked why I would care about using an array instead of a sequence.
I gave a reasonably coherent and objective answer (actually quite
consistent with various approaches to defining true array types in various
existing Python libraries). But don't be concerned. Whatever I think will
have absolutely no impact on the future of Python or your love of it. I've
appreciated all the advice and speculation.
But enough now, eh? This is the *wxPython* list and I have no complaints
about wxPython performance.