Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_gridmap.py
- import sys
- import Tkinter
- def make_canvas(width, height):
- top = Tkinter.Tk()
- top.minsize(width=width + 10, height=height + 10)
- canvas = Tkinter.Canvas(top, width=width, height=height)
- canvas.pack()
- canvas.xview_scroll(6, 'units') # hack so (0, 0) works correctly
- canvas.yview_scroll(6, 'units')
- # draw blue boundaries - sides
- canvas.create_line(width - 1, 0, width - 1, height - 1, fill='blue')
- # top to bottom
- canvas.create_line(0, height - 1, width - 1, height - 1, fill='blue')
- return canvas
- def draw_grid(width, height, n):
- canvas = make_canvas(width, height)
- # Vertical lines at 1/n, 2/n, .. n-1/n
- for i in range(1, n+1):
- x = int(width * i/n)
- if i < n:
- canvas.create_line(x, 0, x, height - 1, fill='red')
- canvas.create_text(x+3, 16, text=str(x), anchor=Tkinter.SW)
- print x
- # b. horizontal lines + text
- for i in range(1, n+1):
- y = int(height * i/n)
- if i < n:
- canvas.create_line(0, y, width - 1, y, fill='red')
- canvas.create_text(3, y, text=str(y), anchor=Tkinter.SW)
- # required last line puts the canvas-window up on screen
- Tkinter.mainloop()
- # Default canvas size
- WIDTH = 900
- HEIGHT = 500
- draw_grid(WIDTH, HEIGHT, 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement