2016-11-29 226 views
0

所以目前我正在制定一個程序,將允許摺紙藝術家在他們的計算機上使用此程序創建摺痕模式。到目前爲止,我有一個程序在畫布上繪製網格,並允許用戶繪製自由曲線,但是,我需要用戶能夠繪製直線,但我不確定如何修改此代碼,以便用戶可以畫直線時不能自由曲調。這是我到目前爲止的代碼:在tkinter畫布上繪製直線與python上的網格與鼠標

from tkinter import * 

Mouse = "up" 
xold, yold = None, None 
def DrawGrid(drawing_area, line_distance): 

    for x in range(line_distance,600,line_distance): 
     drawing_area.create_line(x, 0, x, 600, fill="#d3d3d3") 

    for y in range(line_distance,600,line_distance): 
     drawing_area.create_line(0, y, 600, y, fill="#d3d3d3") 

def main(): 
    root = Tk() 
    drawing_area = Canvas(root, width=600, height=600, bg='white') 
    drawing_area.pack() 
    DrawGrid(drawing_area, 10) 
    drawing_area.bind("<Motion>", motion) 
    drawing_area.bind("<ButtonPress-1>", Mousedown) 
    drawing_area.bind("<ButtonRelease-1>", Mouseup) 

    root.mainloop() 

def Mousedown(event): 
    global Mouse 
    Mouse = "down"   

def Mouseup(event): 
    global Mouse, xold, yold 
    Mouse = "up" 
    xold = None   
    yold = None 

def motion(event): 
    if Mouse == "down": 
     global xold, yold 

    if xold is not None and yold is not None: 
     event.widget.create_line(xold,yold,event.x,event.y,smooth=TRUE) 

    xold = event.x 
    yold = event.y 


main() 

謝謝 Mistry27

回答

0

我的解決辦法是隻讓用戶一次畫一條線。也就是說,隨着鼠標移動,您不會繼續創建新的線條。相反,修改現有的行。

例如,您可以這樣做,以便當用戶單擊然後移動鼠標時,無論鼠標位於何處,從單擊的位置到當前位置只繪製一條線。這將迫使該線始終儘可能直線。

我開始使用您的代碼,並儘可能做少量更改以顯示這可能如何工作。如果您點擊,拖動,點擊,拖動,點擊等,它將在每次點擊之間只繪製一條線段。如果要啓動與當前行完全斷開連接的新行,請按轉義鍵。

在代碼中,變量current包含當前正在繪製的線的ID。當您按換碼時,將重置爲None。無論何時單擊鼠標,都會從current的末尾或當前的鼠標位置開始新行。在運動過程中,我們只是重置當前線的終點。

from tkinter import * 

current = None 

def DrawGrid(drawing_area, line_distance): 

    for x in range(line_distance,600,line_distance): 
     drawing_area.create_line(x, 0, x, 600, fill="#d3d3d3") 

    for y in range(line_distance,600,line_distance): 
     drawing_area.create_line(0, y, 600, y, fill="#d3d3d3") 

def main(): 
    root = Tk() 
    drawing_area = Canvas(root, width=600, height=600, bg='white') 
    drawing_area.pack() 
    DrawGrid(drawing_area, 10) 
    drawing_area.bind("<Escape>", reset) 
    drawing_area.bind("<Motion>", motion) 
    drawing_area.bind("<ButtonPress-1>", Mousedown) 

    root.mainloop() 

def reset(event): 
    global current 
    current = None 

def Mousedown(event): 
    global current 

    event.widget.focus_set() # so escape key will work 

    if current is None: 
     # the new line starts where the user clicked 
     x0 = event.x 
     y0 = event.y 

    else: 
     # the new line starts at the end of the previously 
     # drawn line 
     coords = event.widget.coords(current) 
     x0 = coords[2] 
     y0 = coords[3] 

    # create the new line 
    current = event.widget.create_line(x0, y0, event.x, event.y) 

def motion(event): 
    if current: 
     # modify the current line by changing the end coordinates 
     # to be the current mouse position 
     coords = event.widget.coords(current) 
     coords[2] = event.x 
     coords[3] = event.y 

     event.widget.coords(current, *coords) 

main() 
+0

非常感謝你們的幫助,我的計劃,我需要行用戶取消點擊後可以得出,而不必繼續從之前的最後行的選擇,但我應該能夠用你的例子來解決這個問題。 –