[Tutor] Code now broken with upgrade to 2.1...

Michael P. Reilly arcege@speakeasy.net
Wed, 30 May 2001 07:01:01 -0400 (EDT)


Glen Wheeler wrote
> 
> This is a multi-part message in MIME format.
> 
> ------=_NextPart_000_000E_01C0E8FB.EF654280
> Content-Type: text/plain;
> 	charset="iso-8859-1"
> Content-Transfer-Encoding: quoted-printable
> 
>   Hi all,
> 
>   I upgraded to 2.1, and now it seems this code acts quite differently.
> 
> ###
> from Tkinter import *
> 
> def pjim(jim):
>     print jim
> 
> root =3D Tk()
> tlw =3D Toplevel(root)
> jim =3D 1
> f =3D Frame(tlw, height=3D100, width=3D150)
> Radiobutton(f, text=3D'Path', value=3D50, variable=3Djim).pack()
> Radiobutton(f, text=3D'Alright', value=3D9, variable=3Djim).pack()
> Radiobutton(f, text=3D'Fun', value=3D5, variable=3Djim).pack()
> Radiobutton(f, text=3D'Stupid', value=3D1, variable=3Djim).pack()
> f.pack(padx=3D5, pady=3D5)
> b =3D Button(tlw, text=3D'print jim', command=3Dlambda x=3Djim:pjim(x))
> b.pack()
> mainloop()
> ###
> 
>   Of course, it was alot more complicated originally - so I compressed =
> it down until I have come to this.  For the life of me, I cannot get a =
> little white 'radiobutton' to appear - only the text for each =
> radiobutton.
>   If I change the fg and/or bg (foreground, background) of a single =
> radiobutton then it changes when I click on it...however, using the =
> printjim button it tells me that jim in fact does not change upon =
> clicking just the text.
>   Any ideas or help?

For me, this is working the same for 1.5.2, 2.0 and 2.1.  I see all
the radio buttons, and all the values are always one.  It could be a
platform problem; you didn't say what platform you were testing this on
(mine right now is RedHat Linux 7.0).

The problem that I see is that you are giving a Python variable name to
the Radiobutton widget; Python variables are just references to objects,
not "slots" to store values in.  Therefore you cannot "pass-by-reference"
as you are attempting (here "pass-by-reference" means passing the
address of the variable, not the value in the variable).  Tkinter (on
some platform) might give unexpected results if not given the Tkinter
variable required.

Instead, change your code to:
def pjim(jim):
  print jim.get()
root = Tk()
jim = IntVar(); jim.set(1) # a Tkinter integer variable
...

At this point, the variable jim is pointing to a Tkinter variable that
a) will exist in the Tcl/Tk world for modification, and
b) can be modified independent of the variable name "jim".
(unlike an integer which is immutable).

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |