2015-11-30 26 views
0

我設法創建了一個具有給定半徑,起點和點數的函數它將創建一個大圓圈,並使用此圓圈創建4個小圓圈。 我想在背景上添加一個網格,它將以每100像素的間距顯示TKinter中的Y軸和X軸,從左上角開始,X的正方向向右,Y的正方向向下。 例如,如果屏幕是300X300,那麼tkinter的X軸上將有100 200 300從左到右,從100到300從上到下。 當你用y和x軸繪製一個函數y(x),但是y軸的起點不同和「正確方向」不同時,你有一種類型的網格。如何在python中創建TKINTER上的網格

例如noraml,飽滿,線條畫我使用,從創建的行類,其中包含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 
+0

您遇到什麼問題?這聽起來像你知道如何繪製圓圈。你知道如何畫線嗎?你知道如何創建一個以100爲增量跳轉的循環嗎? –

+0

是的我知道如何創建一條線[或者至少我有一個我認爲我理解的例子]。但是在創建網格時,網格的線條不會像法線。線條被破壞。我的意思是,而不是_________它會是 - - - - - - - 。 – maor

+0

請顯示那個代碼的例子。我們無法修復我們無法看到的代碼。 –

回答

2

這將創建在畫布上的網格爲你

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()