用鼠标指针移动Python龟

7fyelxc5  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(129)

我一直在尝试保存图像捕捉手写文本为我的手写文本识别项目.为此,我使用Python龟.我想改变坐标的龟(在钢笔向上的位置)在画布上通过移动我的鼠标,并让它写(在钢笔向下的位置)通过移动鼠标,同时按住鼠标左键.我不能实现这一点.这里是我的代码.

import tkinter
import turtle

sc = tkinter.Tk()
sc.geometry("1000x1000+100+100")

fr4 = tkinter.Frame(sc, height=500, width=600, bd=4, bg="light green", takefocus="", relief=tkinter.SUNKEN)

fr4.grid(row=2, column=2, sticky=(tkinter.N, tkinter.E, tkinter.W, tkinter.S))

# Canvas
canvas = tkinter.Canvas(fr4, width=1920, height=1080)
canvas.pack()

# Turtle
turtle1 = turtle.RawTurtle(canvas)
turtle1.color("black")
turtle1.shape("turtle")
turtle1.speed(100000)

def drag_handler(x, y):
    turtle1.ondrag(None)  # disable event inside event handler
    turtle1.goto(x, y)
    turtle1.ondrag(drag_handler)  # reenable event on event handler exit

turtle1.ondrag(drag_handler)

sc.mainloop()

字符串

pobjuy32

pobjuy321#

下面是我对你所描述的内容的实现。我已经将它从Tk中移到了turtle中。然而,我引入了低级别的Tk调用来实现缺少的turtle onmove()事件处理程序。一旦就位,它就变成了管理运动,点击,释放和拖动的问题。确保首先点击窗口的标题栏以使其活动:

from turtle import Turtle, Screen

MOVING, DRAGGING = range(2)  # states

def move_handler(x, y):
    if state != MOVING:  # ignore stray events
        return

    onmove(screen, None)  # avoid overlapping events
    yertle.penup()
    yertle.setheading(yertle.towards(x, y))
    yertle.goto(x, y)
    onmove(screen, move_handler)

def click_handler(x, y):
    global state

    yertle.onclick(None)  # disable until release
    onmove(screen, None)  # disable competing handler

    yertle.onrelease(release_handler)  # watch for release event
    yertle.ondrag(drag_handler)  # motion is now dragging until release

    state = DRAGGING

def release_handler(x, y):
    global state

    yertle.onrelease(None)  # disable until click
    yertle.ondrag(None)  # disable competing handler

    yertle.onclick(click_handler)  # watch for click event
    onmove(screen, move_handler)  # dragging is now motion until click

    state = MOVING

def drag_handler(x, y):
    if state != DRAGGING:  # ignore stray events
        return

    yertle.ondrag(None)  # disable event inside event handler
    yertle.pendown()
    yertle.setheading(yertle.towards(x, y))
    yertle.goto(x, y)
    yertle.ondrag(drag_handler)  # reenable event on event handler exit

def onmove(self, fun, add=None):
    """
    Bind fun to mouse-motion event on screen.

    Arguments:
    self -- the singular screen instance
    fun  -- a function with two arguments, the coordinates
        of the mouse cursor on the canvas.

    Example:

    >>> onmove(turtle.Screen(), lambda x, y: print(x, y))
    >>> # Subsequently moving the cursor on the screen will
    >>> # print the cursor position to the console
    >>> screen.onmove(None)
    """

    if fun is None:
        self.cv.unbind('<Motion>')
    else:
        def eventfun(event):
            fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)
        self.cv.bind('<Motion>', eventfun, add)

screen = Screen()
screen.setup(500, 600)
screen.screensize(1920, 1080)

yertle = Turtle('turtle')
yertle.speed('fastest')

state = MOVING

# Initially we track the turtle's motion and left button clicks
onmove(screen, move_handler)  # a la screen.onmove(move_handler)
yertle.onclick(click_handler)  # a click will turn motion into drag

screen.mainloop()

字符串
onmove()事件实现来自我对Find the cursor's current position in Python turtle的回答,当你访问时,请随意给予支持(就像你的drag_handler()来自我对Turtle freehand drawing的回答一样,如果你还没有给予支持,请随意给予支持)。

相关问题