[Tutor] String to integer?
Jeff Shannon
jeff at ccvcorp.com
Wed Sep 1 21:12:46 CEST 2004
adam wrote:
> I have a list which holds all the letters of the alphabet. I want to
> create a button (wxPython) for each of these letters and then give the
> button an ID (count - which is an integer). However, I would like the ID
> to be driven by the list (which is string). See code below.
>
> What's the easiest way of creating an integer which is based on a value of
> a letter? I really don't mind if it was based on the ascii code, or
> another equivalent.
The potential gotcha here, that hasn't been being addressed, is that
if you're using these integers as IDs inside wxPython then you need to
be careful to select numbers that won't conflict with *other* IDs.
One way of doing this would be to select a base ID, near the beginning
of a range that you know is clear, and then defining each button's ID
to be base_id + ord(char).
However, there's rarely any need, in wxPython, to explicitly specify
widget IDs. You're better off using either wxNewId() (or just passing
in -1). If you need the ID for something, you can get it with
wxWidget.GetId(). This does mean that you need to keep object
references to your widgets, but that's probably a good idea anyhow --
you can easily set up a dictionary which maps a letter to a wxButton
instance, and store that dictionary either on your parent window or in
the wxApp instance...
Jeff Shannon
Technician/Programmer
Credit International
More information about the Tutor
mailing list