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