SHARE
TWEET

# Tk_gridmap.py

here2share May 28th, 2020 933 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Tk_gridmap.py
  2.  
  3. import sys
  4. import Tkinter
  5.  
  6.  
  7. def make_canvas(width, height):
  8.     top = Tkinter.Tk()
  9.     top.minsize(width=width + 10, height=height + 10)
  10.  
  11.     canvas = Tkinter.Canvas(top, width=width, height=height)
  12.     canvas.pack()
  13.     canvas.xview_scroll(6, 'units')  # hack so (0, 0) works correctly
  14.     canvas.yview_scroll(6, 'units')
  15.  
  16.     # draw blue boundaries - sides
  17.     canvas.create_line(width - 1, 0, width - 1, height - 1, fill='blue')
  18.     # top to bottom
  19.     canvas.create_line(0, height - 1, width - 1, height - 1, fill='blue')
  20.     return canvas
  21.  
  22.  
  23. def draw_grid(width, height, n):
  24.     canvas = make_canvas(width, height)
  25.  
  26.     # Vertical lines at 1/n, 2/n, .. n-1/n
  27.     for i in range(1, n+1):
  28.         x = int(width * i/n)
  29.         if i < n:
  30.             canvas.create_line(x, 0, x, height - 1, fill='red')
  31.         canvas.create_text(x+3, 16, text=str(x), anchor=Tkinter.SW)
  32.         print x
  33.  
  34.     # b. horizontal lines + text
  35.     for i in range(1, n+1):
  36.         y = int(height * i/n)
  37.         if i < n:
  38.             canvas.create_line(0, y, width - 1, y, fill='red')
  39.         canvas.create_text(3, y, text=str(y), anchor=Tkinter.SW)
  40.  
  41.     # required last line puts the canvas-window up on screen
  42.     Tkinter.mainloop()
  43.  
  44.  
  45. # Default canvas size
  46. WIDTH = 900
  47. HEIGHT = 500
  48.  
  49. draw_grid(WIDTH, HEIGHT, 10)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top