Python 2.6 Graphics Cookbook
上QQ阅读APP看书,第一时间看更新

A straight line and the coordinate system

Draw a straight line on a canvas. It is important to understand that the start of the coordinate system is always at the top left-hand corner of the canvas as shown in the previous figure.

How to do it...

  1. In a text editor type the lines below that appear between the two #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> divider/separators.
  2. Save this as a file named line_1.py, inside the directory called constr again.
  3. As before, open up an X terminal or DOS window if you are using MS Windows.
  4. Change directory (command cd /constr) into the directory constr - where line_1.py is located.
  5. Type python line_1.py and your program will execute. The result should look like the following screenshot:
    How to do it...
    # line_1.py
    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>        
    from Tkinter import *
    root = Tk()
    root.title('Basic Tkinter straight line')
    cw = 800                                    # canvas width, in pixels
    ch = 200                                    # canvas height, in pixels
    canvas_1 = Canvas(root, width=cw, height=ch)
    canvas_1.grid(row=0, column=1)        # placement of the canvas
    x_start = 10                              # bottom left
    y_start = 10
    x_end = 50                              # top right
    y_end = 30
    canvas_1.create_line(x_start, y_start, x_end,y_end)
    root.mainloop()
    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

How it works...

We have written the coordinates for our line differently from the way we did in the previous chapter because we want to introduce symbolic assignments into the create_line() method. This is a preliminary step to making our code re-usable. There is more than one way to specify the points that define the location of line. The neatest way is to define a Python list or tuple by name and then just insert this name of the list as the argument of the create_line() method.

For example, if we wanted to draw two lines, one from (x=50, y=25) to (x= 220, y=44) and the second line between(x=11, y=22) and (x=44, y=33) then we could write the following lines in our program:

  • line_1 = 50, 25, 220, 44 # this is a tuple and can NEVER change
  • line_2 = [11, 22, 44, 33] # this is a list and can be changed anytime.
  • canvas_1.create_line(line_1)
  • canvas_1.create_line(line_2)

Note that although line_1 = 50, 25, 220, 44 is syntactically correct Python, it is considered to be poor Python grammar. It is better to write line_1 = ( 50, 25, 220, 44) because this is more explicit and therefore clearer to someone reading the code. Another point to note is that canvas_1 is an arbitrary name I have given to the particular instance of a canvas of a certain size. You can give it any name you like.

There's more...

Most shapes can be made up of pieces of lines joined together in a multitude of ways. An extremely useful attribute that Tkinter offers is the ability to transform sequences of straight lines into smooth curves. This attribute of lines can be used in surprising ways and is illustrated in recipe 6.