gotta love radio buttons

E

eneskristo

So, I'm having this radio button issue in tkinter:
First I assign the IntVar:
var = []
while i < self.something:
var.append(IntVar())
i += 2
Later on I use them, but I get this error:
for r in var:
helper = var[r].get()
self.something_else[helper] += 1
Then, this happens:
Traceback (most recent call last):
File "F:\Portable Python 3.2.5.1\App\lib\tkinter\__init__.py", line 1456, in __call__
return self.func(*args)
File "----(Not giving this)", line 26, in submit_data
helper = var[r].get()
TypeError: list indices must be integers, not IntVar
I'm willing to give additional info. Thank you in advance.
 
R

Roy Smith

I don't use tkinter, but here's what I can figure out from looking at
your code and http://effbot.org/tkinterbook/variable.htm
var = []
while i < self.something:
var.append(IntVar())
i += 2

At this point, var is a list of IntVar instances.
for r in var:
helper = var[r].get()
self.something_else[helper] += 1

You are iterating over the element of var, so each time through the
loop, r is an IntVar instance. But, you're using r to index a list in
helper = var[r].get()

That's what
TypeError: list indices must be integers, not IntVar

means. I suspect what you want is to retrieve the integer value from r,
and use that as the index:
helper = var[r.get()]

but without knowing more about your code, that's just a guess.

At a deeper level, however, there's something that fundamentally doesn't
make sense here. You are iterating over the values in var, then using
each value as an index again. That's not *wrong*, but it seems unlikely
to be what you want.
 
N

Ned Batchelder

So, I'm having this radio button issue in tkinter:
First I assign the IntVar:
var = []
while i < self.something:
var.append(IntVar())
i += 2
Later on I use them, but I get this error:
for r in var:
helper = var[r].get()
self.something_else[helper] += 1
Then, this happens:
Traceback (most recent call last):
File "F:\Portable Python 3.2.5.1\App\lib\tkinter\__init__.py", line 1456, in __call__
return self.func(*args)
File "----(Not giving this)", line 26, in submit_data
helper = var[r].get()
TypeError: list indices must be integers, not IntVar
I'm willing to give additional info. Thank you in advance.

This isn't about radio buttons, it's about how for loops work. I think
you want:

for r in var:
helper = r.get()

The iteration variable in a for loop (r in this case) takes on the
values of the elements of the list, not the indexes of the elements.
 
G

Gary Herron

So, I'm having this radio button issue in tkinter:
First I assign the IntVar:
var = []
while i < self.something:
var.append(IntVar())
i += 2
Later on I use them, but I get this error:
for r in var:
helper = var[r].get()
self.something_else[helper] += 1
Then, this happens:
Traceback (most recent call last):
File "F:\Portable Python 3.2.5.1\App\lib\tkinter\__init__.py", line 1456, in __call__
return self.func(*args)
File "----(Not giving this)", line 26, in submit_data
helper = var[r].get()
TypeError: list indices must be integers, not IntVar
I'm willing to give additional info. Thank you in advance.


These two lines

for r in var:
helper = var[r].get()

are being redundant.

The loop returns elements from the list (one-by-one). Also var[r]
attempts to return an element from the list (indexed by r -- expected to
be an integer).

Either of these remove the redundancy (but the first is more Pythonic)

for r in var:
helper = r.get()

or

for i in range(len(var)):
helper = var.get()

Gary Herron
 
K

Kev Dwyer

So, I'm having this radio button issue in tkinter:
First I assign the IntVar:
var = []
while i < self.something:
var.append(IntVar())
i += 2
Later on I use them, but I get this error:
for r in var:
helper = var[r].get()
self.something_else[helper] += 1
Then, this happens:
Traceback (most recent call last):
File "F:\Portable Python 3.2.5.1\App\lib\tkinter\__init__.py", line
1456, in __call__
return self.func(*args)
File "----(Not giving this)", line 26, in submit_data
helper = var[r].get()
TypeError: list indices must be integers, not IntVar
I'm willing to give additional info. Thank you in advance.


(untested)
for r in var:
helper = var[r.get()]

I think you need to call get on the IntVar instance to get an int that can
be used to index the list.
 
E

eneskristo

Now it is giving me this error, after changing to helper = var[r.get()]
line 27, in submit_data
self.something_else[r][1] += 1
TypeError: list indices must be integers, not IntVar
 
G

Gary Herron

Now it is giving me this error, after changing to helper = var[r.get()]
line 27, in submit_data
self.something_else[r][1] += 1
TypeError: list indices must be integers, not IntVar

In such an easy case, you really ought to be able to read the error and
understand it rather than needing to rely on us to do that for you.

The message:
List indices must be integers, not IntVar
clearly indicates you are indexing a list with something of type IntVar
instead of the required int. That would have to be the ...[r]. The
value of r is *not* an integer, it's an IntVar which is container of an
int but not an int itself. You can access the contained int with
r.get(), so perhaps ...[r.get()] is what you want. (Or perhaps not...
We really don't know what you are trying to do here.)


Reading error messages and understanding tracebacks are skills well
worth trying to develop. Good luck.

Gary Herron
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top