如何在 python 中的 tkinter 上创建网格?

Man*_*ack 0 tkinter python-2.7

我设法创建了一个具有给定半径、起点和多个点的函数。它将创建一个大圆圈,并使用这个圆圈创建 4 个小圆圈。我想在背景上添加一个网格,该网格将从左上角开始每隔 100 个像素显示 TKinter 中的 Y 轴和 X 轴。坐标原点应该是左上角。例如,如果屏幕是 300x300,那么窗口将在其 X 轴上有 3 条线(在 100、200 和 300),从左到右,从上到下。作为坐标系的网格。

我如何创建法线的示例。我使用包含 2 个点起点和终点的线类:

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
Run Code Online (Sandbox Code Playgroud)

Ste*_*ers 5

这将为您在画布上创建一个网格

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()
Run Code Online (Sandbox Code Playgroud)