[Edu-sig] IDLE problems with Tkinter

Gregor Lingl glingl@aon.at
Fri, 22 Feb 2002 22:49:55 +0100


> 
> Is this a known problem with IDLE? 
> 

Yes. Despite of this I use IDLE with my 
students interactivly.
Here an example session:

>>> from Tkinter import *
>>> root = Tk()
>>> cv = Canvas(root, width=300, height=250, bg='yellow')
>>> cv.pack()
>>> cv.pack_forget()
>>> cv.pack(expand=1, fill=BOTH)
>>> from Canvas import *
>>> r = Rectangle(cv, 50,20, 250,220)
>>> k = Oval(cv, 70,40,230,200, fill='pink')
>>> l1 = Line(cv, 120,150,180,150, fill='red', width=6)
>>> l1.move(0,20)
>>> l1.coords()
[120.0, 170.0, 180.0, 170.0]
>>> l2=Line(cv,150,120,160,162)
>>> l2['fill']='brown'
>>> l2['width']=4
>>> e1=Oval(cv,110,90,140,105,fill='blue')
>>> e2=Oval(cv,159,89,191,106,fill='lightblue')
>>> k['fill']='orange'
>>> 

Works fine and you can observe the construction of
the graphics on the canvas. (In a similar way it's
possible to add Buttons and other widgets etc.)
Students (age of 15) have much fun with this approach.
For them this is a way to explore Tkinter, (which is
very poorly documented in German.)

To turn it into a program we copy (as usual) the
appropriate statements into an edit-window of IDLE
and in this case we got:

from Tkinter import *
from Canvas import *
root = Tk()
cv = Canvas(root, width=300, height=250, bg='yellow')
cv.pack(expand=1, fill=BOTH)
r = Rectangle(cv, 50,20, 250,220)
k = Oval(cv, 70,40,230,200, fill='orange')
l1 = Line(cv, 120,170,180,170, fill='red', width=6)
l2=Line(cv,150,120,160,162)
l2['fill']='brown'
l2['width']=4
e1=Oval(cv,110,90,140,105,fill='blue')
e2=Oval(cv,159,89,191,106,fill='lightblue')

this can be run as usual via File/Save and
Edit/Run script

Another Tk-window pops up and we see our graphics again.

To run this program as a script one only has to add
root.mainloop()

Shortly this means: as long as there is no mainloop()
in your script it runs correctly from IDLE. If later
we want to use it as a script, we add mainloop()

Hope you found this interesting

Gregor Lingl