[Tutor] importing graphics

Gregor Lingl glingl@aon.at
Sun Feb 9 11:25:03 2003


This is a multi-part message in MIME format.
--------------090203060702020800060306
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

WCOLtd@aol.com schrieb:

>I really want to know how to import something like a circle or a line in python.
>
Hi Andrew!

To do what you ask for, you need some Graphics Toolkit. There are
many of them for Python.
The one, which comes with the original Python Distribution from 
www.python.org
is Tkinter, an interface to Tk ... (from Tcl/Tk).
Tkinter is a Toolkit for building  graphical user-interfaces (GUIs), 
which are assembled
with widgets (Window-Gadgets such as Buttons, Labels, Sliders etc.).
For your intentions the appropriate widget is the Canvas-Widget, which 
has methods
to draw several geometrical objects (such as lines, rectangles, circles 
etc.)

To use Tkinter, unfortunatly you have to study a little ... ;-)

Just to let you get an impression I show you a simple script (from M. Lutz's
Programming Python, O'Reilly):

# demo all basic canvas interfaces
from Tkinter import *

canvas = Canvas(width=300, height=300, bg='white')   # 0,0 is top left 
corner
canvas.pack(expand=YES, fill=BOTH)                   # increases down, right

canvas.create_line(100, 100, 200, 200)               # fromX, fromY, 
toX, toY
canvas.create_line(100, 200, 200, 300)               # draw shapes
for i in range(1, 20, 2):
    canvas.create_line(0, i, 50, i)

canvas.create_oval(10, 10, 200, 200, width=2, fill='blue')
canvas.create_arc(200, 200, 300, 100)
canvas.create_rectangle(200, 200, 300, 300, width=5, fill='red')
canvas.create_line(0, 300, 150, 150, width=10, fill='green')

photo=PhotoImage(file='../gifs/guido.gif')
canvas.create_image(250, 0, image=photo, anchor=NW)  # embed a photo

widget = Label(canvas, text='Spam', fg='white', bg='black')
widget.pack()
canvas.create_window(100, 100, window=widget)        # embed a widget
canvas.create_text(100, 280, text='Ham')             # draw some text
mainloop()
 
I've attached it - so you can run it immediately.  Alternatively you can
type it into IDLE line by line, so you can see what each line does.
(But in this case avoid to type in the last line: mainloop() !)

A gentle introduction to Tkinter you can find at "Thinking in Tkinter":

http://home.att.net/~stephen_ferg/thinking_in_tkinter/index.html

This website also contains a list of  important links to docs and tutorials
for Tkinter.

A commented list of links to other GUI - toolkits you can find at:

http://phaseit.net/claird/comp.lang.python/python_GUI.html

Hope that helps

Gregor



>
>I would really appreciate help.
>-Andrew Leister-Frazier
>(Wcoltd@aol.com)
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>



--------------090203060702020800060306
Content-Type: text/plain;
 name="canvas1.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="canvas1.py"

# demo all basic canvas interfaces
from Tkinter import *

canvas = Canvas(width=300, height=300, bg='white')   # 0,0 is top left corner
canvas.pack(expand=YES, fill=BOTH)                   # increases down, right

canvas.create_line(100, 100, 200, 200)               # fromX, fromY, toX, toY
canvas.create_line(100, 200, 200, 300)               # draw shapes
for i in range(1, 20, 2): 
    canvas.create_line(0, i, 50, i)

canvas.create_oval(10, 10, 200, 200, width=2, fill='blue')
canvas.create_arc(200, 200, 300, 100)
canvas.create_rectangle(200, 200, 300, 300, width=5, fill='red')
canvas.create_line(0, 300, 150, 150, width=10, fill='green')

photo=PhotoImage(file='../gifs/guido.gif')
canvas.create_image(250, 0, image=photo, anchor=NW)  # embed a photo

widget = Label(canvas, text='Spam', fg='white', bg='black')
widget.pack()
canvas.create_window(100, 100, window=widget)        # embed a widget
canvas.create_text(100, 280, text='Ham')             # draw some text
mainloop()

--------------090203060702020800060306--