使用鼠标在python上的Grid上的Tkinter画布上绘制直线 [英] Drawing straight lines on a tkinter canvas with Grid on python with mouse

查看:93
本文介绍了使用鼠标在python上的Grid上的Tkinter画布上绘制直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我目前正在开发一个程序,该程序将允许折纸艺术家使用该程序在其计算机上创建折痕图案.到目前为止,我有一个程序可以在画布上绘制网格并允许用户绘制自由线,但是,我要求用户能够绘制直线,但是我不确定如何适应此代码,因此用户可以绘制直线而不是自由曲线的花样.到目前为止,这是我的代码:

so currently I am working on a program that will allow origami artists to create crease patterns on their computer using this program. So far I have a program that draws a grid on the canvas and allows the user to draw a freeform line , however, I require the user to able to draw straight lines but I'm unsure on how to adapt this code so the user can draws straight lines not freeform squiggle. Here is my code so far:

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

Thank you, Mistry27

推荐答案

我的解决方案是只允许用户一次绘制一条线.也就是说,随着鼠标的移动,您不会继续创建新行.而是,修改现有的行.

My solution is to only let the user draw one line at a time. That is, as the mouse moves you don't keep creating new lines. Instead, modify the existing line.

例如,您可以这样做,以便用户单击然后移动鼠标时,无论鼠标位于何处,都只会从单击位置到当前位置绘制一条直线.这将迫使线始终尽可能笔直.

For example, you could make it so that when the user clicks and then moves the mouse, no matter where the mouse is, only a single line is drawn from where they clicked to the current position. That will force the line to always be as straight as possible.

我从您的代码开始,并进行了尽可能少的更改以显示其工作方式.如果单击,拖动,单击,拖动,单击等,则每次单击之间只会绘制一条线段.如果要开始与当前行完全断开连接的新行,请按 escape 键.

I started with your code and made as few changes as possible to show how this might work. If you click, drag, click, drag, click, etc, it will draw only one line segment between each click. If you want to start a new line completely disconnected from the current line, press the escape key.

在代码中,变量 current 包含当前正在绘制的线的ID.当您按 escape 时,它将重置为 None .每当您单击鼠标时,都会从 current 的末尾或当前鼠标位置开始换行.在运动过程中,我们仅重置当前行的端点.

In the code, the variable current contains the id of the line currently being drawn. When you press escape, this is reset to None. Whenever you click the mouse, a new line is started either from the end of current or the current mouse position. During a motion, we merely reset the endpoint of the current line.

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

这篇关于使用鼠标在python上的Grid上的Tkinter画布上绘制直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆