Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用鼠标指针移动蟒蛇龟_Python_Python 3.x_Turtle Graphics - Fatal编程技术网

Python 用鼠标指针移动蟒蛇龟

Python 用鼠标指针移动蟒蛇龟,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我一直在尝试为我的手写文本识别项目保存捕获手写文本的图像。为此,我使用了python海龟。我想通过移动鼠标来更改海龟在画布上的坐标(在向上画笔的位置),并在按住鼠标左键的同时移动鼠标来使海龟书写(在向下画笔的位置)。我无法实现这一点。这是我的密码 import tkinter import turtle sc = tkinter.Tk() sc.geometry("1000x1000+100+100") fr4 = tkinter.Frame(sc, height=500, width=60

我一直在尝试为我的手写文本识别项目保存捕获手写文本的图像。为此,我使用了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()

以下是我对您所描述内容的实现。我把它从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()
从海龟导入海龟,屏幕
移动、拖动=范围(2)#状态
def move_处理器(x,y):
如果是州!=移动:#忽略杂散事件
返回
onmove(屏幕,无)#避免重复事件
耶特尔·彭普
耶特尔.设定航向(耶特尔.朝向(x,y))
耶特尔后藤(x,y)
onmove(屏幕,移动处理程序)
def点击处理程序(x,y):
全球国家
yertle.onclick(无)#在释放前禁用
onmove(屏幕,无)#禁用竞争处理程序
onrelease(release_handler)#监视释放事件
yertle.ondrag(拖动处理程序)#运动现在正在拖动,直到释放
状态=拖动
def释放处理器(x,y):
全球国家
yertle.onrelease(无)#在单击之前禁用
yertle.ondrag(无)#禁用竞争处理程序
yertle.onclick(点击处理程序)#观察点击事件
onmove(屏幕,移动处理程序)#拖动现在是运动,直到单击为止
状态=移动
def拖放处理器(x,y):
如果是州!=拖动:#忽略偶然事件
返回
yertle.ondrag(无)#禁用事件处理程序内的事件
yertle.pendown()
耶特尔.设定航向(耶特尔.朝向(x,y))
耶特尔后藤(x,y)
ondrag(拖放处理程序)#事件处理程序退出时可重新启动事件
def onmove(自我、乐趣、添加=无):
"""
将乐趣绑定到屏幕上的鼠标运动事件。
论据:
self——单一屏幕实例
fun——一个有两个参数的函数,坐标
鼠标光标在画布上的移动。
例子:
>>>onmove(turtle.Screen(),lambda x,y:print(x,y))
>>>#随后在屏幕上移动光标将
>>>#将光标位置打印到控制台
>>>screen.onmove(无)
"""
如果没有乐趣:
自身cv解除绑定(“”)
其他:
def eventfun(事件):
乐趣(self.cv.canvasx(event.x)/self.xscale,-self.cv.canvasy(event.y)/self.yscale)
self.cv.bind(“”,eventfun,add)
screen=screen()
屏幕设置(500600)
屏幕尺寸(19201080)
耶特尔=乌龟(“乌龟”)
耶特尔速度(“最快”)
状态=移动
#最初,我们跟踪海龟的运动并点击左键
onmove(屏幕,移动处理器)#一个la屏幕。onmove(移动处理器)
onclick(点击处理程序)#点击会将运动转化为阻力
screen.mainloop()

onmove()
事件实现源于我的答案,请在访问时自由投票。(正如您的
drag_handler()
来自我的答案,如果您还没有投票,请随时向上投票。)

请更清楚地说明问题的性质,而不是“我无法实现这一点”。到底是什么事情没有按你所希望的那样发生?如果有错误,什么是完全回溯?看,如果你想用鼠标拖动乌龟,你的代码是有效的mouse@eyllanesc,重读他的描述。他希望tutle始终跟踪鼠标,只在鼠标左键按下时留下轨迹。提供的代码可以拖拽,但在没有按下鼠标按钮时不会跟踪。当我的鼠标在笔下位置移动缓慢时,此实现可以完美工作,但如果更随意(如某人的书写),则会出现错误。有时,海龟不会留下痕迹,有时它只在拖动的初始位置和最终位置之间画一条直线。@初学者,我检测了代码,发现它生成了意外的运动事件,所以我添加了一个显式状态变量来过滤掉这些偏差。它画得更干净——如果你需要它是完美的,你可以研究信号量和锁定。非常好的解决方案!谢谢。我想不出la屏幕应该是什么。@AnnZen,我想不出你的评论。
onmove()
代码是以方法的形式编写的,因此尽管我们通过
onmove(screen,move\u handler)
将其作为函数调用,但它可以作为
screen
的方法安装,并在子类中或通过其他一些技巧调用
screen.onmove(move\u handler)