How to solve "TypeError: list indices must be integers".

Ã

ÃCø¯

This is more details about my problem, which I running my py script
for my project. Programming in pythoncard that we can develop a GUI
based application easily.

I was assigned dialog.colorDialog(self) return value to a result
object, but I suspect that result.color is the attribute of the result
object that can assign to a string variable.

There is a error prompt from python console "TypeError: list indices
must be integers".
Have any suggestion to solve this problem?

When I print result.color, it is print out something like (255,0,0).
How to covert result.color into a string? How to convert a string to
result.color type?

adrian
 
D

Dennis Lee Bieber

I was assigned dialog.colorDialog(self) return value to a result
object, but I suspect that result.color is the attribute of the result
object that can assign to a string variable.
Showing the actual code would help...
There is a error prompt from python console "TypeError: list indices
must be integers".
Have any suggestion to solve this problem?
Make sure you only have an integer when subscripting into a list?
When I print result.color, it is print out something like (255,0,0).

Looks like a tuple -- probably
(red-level, green-level, blue-level)
where *-level is in the range 0..255 (x00..xFF); your example would be
(full red, no green, no blue)
How to covert result.color into a string? How to convert a string to
result.color type?
What type of string?
"0x%2.2X%2.2X%2.2X" % (r, g, b)
will produce a hex string of the form
0xFF0000
given your sample RGB

If you want to get "Red", you'll need a look-up table and probably
some least-distance error function for items that don't directly match.

(255, 0, 0) Red
(255, 255, 0) Yellow
(255, 0, 255) Magenta
(0, 255, 0) Green
(0, 255, 255) Cyan
(0, 0, 255) Blue
(0, 0, 0) Black
(255, 255, 255) White

But where would you put: (127, 127, 63) [A half-black Yellow
with 1/4 blue added].. Is it closer to Yellow, or to Black: 255-127 =>
128, but 127 - 0 => 127... Shorter simplistic distance means map this to
(0, 0, 0)... But (128, 128, 64) simplistic shorter distance would map to
Yellow

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

John Machin

This is more details about my problem, which I running my py script
for my project. Programming in pythoncard that we can develop a GUI
based application easily.

I was assigned dialog.colorDialog(self) return value to a result
object, but I suspect that result.color is the attribute of the result
object that can assign to a string variable.

The concepts "<developer> assigned <expression> to a <type> object"
and "<expression> can be assigned to a <type> variable" just don't
exist in Python.

# Initially "name1" isn't bound to anything
name1 = 42
# "name1" now refers to an int object whose value is 42
name1 = 'abc'
# "name1" now refers to a str object whose value is 'abc'
name2 = name1
# "name2" now refers to the same str object

What is "dialog.colorDialog(self) return value"? What is "a result
object"? Show us the code!

What is the connection between your last sentence above and the
"TypeError: list indices must be integers" problem? Show us the code!
There is a error prompt from python console "TypeError: list indices
must be integers".
Have any suggestion to solve this problem?

Communication would be much easier if you show us the line of code
that causes the error message.

Here are two simple examples of what can trigger that error message:
a_list = [1, 42, 666]
not_an_integer = None
a_list[not_an_integer] = 9876
Traceback (most recent call last):
a_name = a_list[not_an_integer]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers

Look for the pattern a_list[not_an_integer] in the statement that
triggers the exception.

When I print result.color, it is print out something like (255,0,0).

Yes, that's most likely a tuple of (red, green, blue) values ... I'm
not astonished; are you?
How to covert result.color into a string?

How? Use elementary Python functionality, after you've decided what
string representation you want. Examples:
How to convert a string to
result.color type?

Reverse the process.

Again, what is the connection between "result.color" and the
"TypeError: list indices must be integers" problem?
 
Ã

ÃCø¯

This is more details about my problem, which I running my py script
for my project. Programming in pythoncard that we can develop a GUI
based application easily.
I was assigned dialog.colorDialog(self) return value to a result
object, but I suspect that result.color is the attribute of the result
object that can assign to a string variable.

The concepts "<developer> assigned <expression> to a <type> object"
and "<expression> can be assigned to a <type> variable" just don't
exist in Python.

# Initially "name1" isn't bound to anything
name1 = 42
# "name1" now refers to an int object whose value is 42
name1 = 'abc'
# "name1" now refers to a str object whose value is 'abc'
name2 = name1
# "name2" now refers to the same str object

What is "dialog.colorDialog(self) return value"? What is "a result
object"? Show us the code!

What is the connection between your last sentence above and the
"TypeError: list indices must be integers" problem? Show us the code!


There is a error prompt from python console "TypeError: list indices
must be integers".
Have any suggestion to solve this problem?

Communication would be much easier if you show us the line of code
that causes the error message.

Here are two simple examples of what can trigger that error message:
a_list = [1, 42, 666]
not_an_integer = None
a_list[not_an_integer] = 9876

Traceback (most recent call last):
a_name = a_list[not_an_integer]

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers

Look for the pattern a_list[not_an_integer] in the statement that
triggers the exception.


When I print result.color, it is print out something like (255,0,0).

Yes, that's most likely a tuple of (red, green, blue) values ... I'm
not astonished; are you?
How to covert result.color into a string?

How? Use elementary Python functionality, after you've decided what
string representation you want. Examples:

'red=255 green=128 blue=0'>>> '.someclass {background-color: #%02x%02x%02x; }' % color

'.someclass {background-color: #ff8000; }'


How to convert a string to
result.color type?

Reverse the process.

Again, what is the connection between "result.color" and the
"TypeError: list indices must be integers" problem?

Many thanks, John Machin for your fast reply!
Regards,
Andreas
 
Ã

ÃCø¯

I was assigned dialog.colorDialog(self) return value to a result
object, but I suspect that result.color is the attribute of the result
object that can assign to a string variable.

        Showing the actual code would help...
There is a error prompt from python console "TypeError: list indices
must be integers".
Have any suggestion to solve this problem?

        Make sure you only have an integer when subscripting into a list?
When I print  result.color, it is print out something like (255,0,0).

        Looks like a tuple -- probably
                (red-level, green-level, blue-level)
where *-level is in the range 0..255 (x00..xFF); your example would be
(full red, no green, no blue)
How to covert result.color into a string?  How to convert a string to
result.color type?

        What type of string?
                "0x%2.2X%2.2X%2.2X" % (r, g, b)
will produce a hex string of the form
                0xFF0000
given your sample RGB

        If you want to get "Red", you'll need a look-up table and probably
some least-distance error function for items that don't directly match.

(255, 0, 0)             Red
(255, 255, 0)   Yellow
(255, 0, 255)   Magenta
(0, 255, 0)             Green
(0, 255, 255)   Cyan
(0, 0, 255)             Blue
(0, 0, 0)               Black
(255, 255, 255) White

        But where would you put:        (127, 127, 63)          [A half-black Yellow
with 1/4 blue added].. Is it closer to Yellow, or to Black: 255-127 =>
128, but 127 - 0 => 127... Shorter simplistic distance means map this to
(0, 0, 0)... But (128, 128, 64) simplistic shorter distance would map to
Yellow

--
        Wulfraed        Dennis Lee Bieber               KD6MOG
        (e-mail address removed)                (e-mail address removed)
                HTTP://wlfraed.home.netcom.com/
        (Bestiaria Support Staff:               (e-mail address removed))
                HTTP://www.bestiaria.com/

Many thanks, Dennis Lee Bieber for your fast reply!
Regards,
Andreas
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,297
Latest member
EngineerD

Latest Threads

Top