0

I managed to create a function that with a given radius, starting point and a number of points. It will create a big circle and withing this circle it will create 4 small circles. I want to add a grid on the background that will show the Y and X axis in TKinter every 100 pixels apart starting from the top left. The coordinate origin should be the top left corner. For example if the screen is 300x300 then the window will have 3 lines (at 100, 200 and 300) on his X axis going from left to right and top up to bottom. A grid as a coordinate system.

Example of how I create a normal line. I use a line class which contains 2 points start point and end point:

rootWindow = Tkinter.Tk()
rootFrame = Tkinter.Frame(rootWindow, width=1000, height=800, bg="white")
rootFrame.pack()
canvas = Tkinter.Canvas(rootFrame, width=1000, height=800, bg="white")
canvas.pack() 
def draw_line(l):
    "Draw a line with its two end points"
    draw_point(l.p1)
    draw_point(l.p2)
    # now draw the line segment
    x1 = l.p1.x
    y1 = l.p1.y
    x2 = l.p2.x
    y2 = l.p2.y
    id = canvas.create_line(x1, y1, x2, y2, width=2, fill="blue")
    return id
Nummer_42O
  • 334
  • 2
  • 16
Man in Black
  • 89
  • 2
  • 2
  • 12
  • What part of the problem are you struggling with? It sounds like you know how to draw circles. Do you know how to draw lines? Do you know how to create a loop that jumps in increments of 100? – Bryan Oakley Nov 30 '15 at 19:35
  • yes I know how to create a line [or atleast I have an example of it which I think I understand ] . but when creating a grid the lines of the grid arent like a normal line . the lines are broken . I mean instead of _________ it will be - - - - - - - . – Man in Black Nov 30 '15 at 19:49
  • please show an example of the code that does that. We can't fix code that we can't see. – Bryan Oakley Nov 30 '15 at 20:10
  • @BryanOakley coudlnt show the code normally in comment so I edit it in the main question post . – Man in Black Nov 30 '15 at 20:15
  • That code won't run as posted -- there are several indentation errors. Plus, it's only drawing a single line, not a grid. – Bryan Oakley Nov 30 '15 at 20:42

1 Answers1

7

This will create a grid on the canvas for you

import tkinter as tk

def create_grid(event=None):
    w = c.winfo_width() # Get current width of canvas
    h = c.winfo_height() # Get current height of canvas
    c.delete('grid_line') # Will only remove the grid_line

    # Creates all vertical lines at intevals of 100
    for i in range(0, w, 100):
        c.create_line([(i, 0), (i, h)], tag='grid_line')

    # Creates all horizontal lines at intevals of 100
    for i in range(0, h, 100):
        c.create_line([(0, i), (w, i)], tag='grid_line')

root = tk.Tk()

c = tk.Canvas(root, height=1000, width=1000, bg='white')
c.pack(fill=tk.BOTH, expand=True)

c.bind('<Configure>', create_grid)

root.mainloop()
Steven Summers
  • 5,079
  • 2
  • 20
  • 31