Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63205 > unrolled thread

gotta love radio buttons

Started byeneskristo@gmail.com
First post2014-01-05 10:18 -0800
Last post2014-01-05 10:38 -0800
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  gotta love radio buttons eneskristo@gmail.com - 2014-01-05 10:18 -0800
    Re: gotta love radio buttons Roy Smith <roy@panix.com> - 2014-01-05 13:33 -0500
    Re: gotta love radio buttons Ned Batchelder <ned@nedbatchelder.com> - 2014-01-05 13:37 -0500
    Re: gotta love radio buttons Kev Dwyer <kevin.p.dwyer@gmail.com> - 2014-01-05 18:40 +0000
    Re: gotta love radio buttons eneskristo@gmail.com - 2014-01-05 10:47 -0800
      Re: gotta love radio buttons Gary Herron <gary.herron@islandtraining.com> - 2014-01-05 11:51 -0800
    Re: gotta love radio buttons Gary Herron <gary.herron@islandtraining.com> - 2014-01-05 10:38 -0800

#63205 — gotta love radio buttons

Fromeneskristo@gmail.com
Date2014-01-05 10:18 -0800
Subjectgotta love radio buttons
Message-ID<8dca57e8-8258-4020-9788-987af332b5b2@googlegroups.com>
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.

[toc] | [next] | [standalone]


#63206

FromRoy Smith <roy@panix.com>
Date2014-01-05 13:33 -0500
Message-ID<roy-EBB615.13331205012014@news.panix.com>
In reply to#63205
In article <8dca57e8-8258-4020-9788-987af332b5b2@googlegroups.com>,
 eneskristo@gmail.com wrote:

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.

[toc] | [prev] | [next] | [standalone]


#63207

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-01-05 13:37 -0500
Message-ID<mailman.4955.1388947062.18130.python-list@python.org>
In reply to#63205
On 1/5/14 1:18 PM, eneskristo@gmail.com wrote:
> 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.

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [next] | [standalone]


#63208

FromKev Dwyer <kevin.p.dwyer@gmail.com>
Date2014-01-05 18:40 +0000
Message-ID<mailman.4956.1388947262.18130.python-list@python.org>
In reply to#63205
eneskristo@gmail.com wrote:

> 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.


[toc] | [prev] | [next] | [standalone]


#63209

Fromeneskristo@gmail.com
Date2014-01-05 10:47 -0800
Message-ID<1dffaef8-ba59-4bbf-b8ac-02c611640f92@googlegroups.com>
In reply to#63205
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

[toc] | [prev] | [next] | [standalone]


#63216

FromGary Herron <gary.herron@islandtraining.com>
Date2014-01-05 11:51 -0800
Message-ID<mailman.4962.1388951520.18130.python-list@python.org>
In reply to#63209
On 01/05/2014 10:47 AM, eneskristo@gmail.com wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#63210

FromGary Herron <gary.herron@islandtraining.com>
Date2014-01-05 10:38 -0800
Message-ID<mailman.4957.1388947598.18130.python-list@python.org>
In reply to#63205
On 01/05/2014 10:18 AM, eneskristo@gmail.com wrote:
> 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[i].get()

Gary Herron

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web