Lines in Tkinter
Back to index
This is the first assignment in which Tkinter is used. Whereas the turtle
graphics module is not all that popular, the Tkinter package is the most
used GUI (Graphical User Interface) when it comes to Python. Get the
following sample program up and running.
#!/usr/bin/python
from tkinter import *
tk = Tk()
canvas = Canvas(tk,width=300, height=300, background="black")
canvas.pack()
canvas.create_line(0,0,150,150,fill="cyan", width=3)
canvas.create_line(300,0,150,150,fill="yellow", width=3)
canvas.create_line(300,300,150,150,fill="pink", width=3)
canvas.create_line(0,300,150,150,fill="magenta", width=3)
for x in range(160,300,10):
canvas.create_line(150-(x-150),x,x,x,fill="white")
canvas.create_text(145,15,text="N", fill="red")
mainloop()
Certain lines in this sample program will appear in one form or another in
any Python program utilizing Tkinter. Obviously all or part of Tkinter must
be imported, but less obviously four key lines must appear:
master = Tk()
w = Canvas(master, width=400, height=300)
w.pack()
...
mainloop()
The rest of the program utilizes constructs already introduced in previous
lessons or is otherwise fairly self-explanatory. You should experiment
with create_line to make sure you understand how it works. Actually,
mainloop() is optional in static programs such as the demo program shown
above.
ASSIGNMENT:
Make the following changes to the sample program:
- Using the method demonstrated in the sample program, place vertical
lines in the left and right quadrants
- Place horizontal lines in the top quadrant to match those in the bottom
quadrant.
- Place W, E, and S as appropriate to match the N in the sample
program
|