简体   繁体   English

Python Turtle-单击事件

[英]Python Turtle - Click Events

I'm currently making a program in python's Turtle Graphics. 我目前正在使用python的Turtle Graphics编写程序。 Here is my code in case you need it 这是我的代码,以防您需要

import turtle
turtle.ht()

width = 800
height = 800
turtle.screensize(width, height)

##Definitions
def text(text, size, color, pos1, pos2):
     turtle.penup()
     turtle.goto(pos1, pos2)
     turtle.color(color)
     turtle.begin_fill()
     turtle.write(text, font=('Arial', size, 'normal'))
     turtle.end_fill()

##Screen
turtle.bgcolor('purple')
text('This is an example', 20, 'orange', 100, 100)


turtle.done()

I want to have click events. 我想要点击事件。 So, where the text 'This is an example' is wrote, I want to be able to click that and it prints something to the console or changes the background. 因此,在写有'This is an example'的文本'This is an example'地方,我希望能够单击该文本并将其打印到控制台或更改背景。 How do I do this? 我该怎么做呢?

EDIT: 编辑:

I don't want to install anything like pygame, it has to be made in Turtle 我不想安装类似pygame的东西,它必须在Turtle中制作

Use the onscreenclick method to get the position then act on it in your mainloop (to print or whatever). 使用onscreenclick方法获取位置,然后在您的主循环中对其进行操作(打印或执行其他操作)。

import turtle as t

def main():
    t.onscreenclick(getPos)
    t.mainloop()
main()

Also see : Python 3.0 using turtle.onclick Also see : Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates 另请参阅: 使用turtle.onclick的Python 3.0另请参见: python中的Turtle- 试图使Turtle移动到鼠标单击位置并打印其坐标

Since your requirement is to have onscreenclick around text area, we need to track mouse position. 由于您的要求是要在文本区域周围进行onscreenclick ,因此我们需要跟踪鼠标的位置。 For that we are binding function onTextClick to the screenevent. 为此,我们将功能onTextClick绑定到screenEvent。 Within function, if we are anywere around text This is an example , a call is made to turtle.onscreenclick to change color of the background to red . 在函数内,如果我们在文本周围, This is an exampleturtle.onscreenclick调用turtle.onscreenclick将背景颜色更改为red You can change lambda function and insert your own, or just create external function and call within turtle.onscreenclick as per this documentation 您可以根据本文档更改lambda函数并插入自己的函数,也可以创建外部函数并在turtle.onscreenclick调用

I tried to change your code as little as possible. 我试图尽可能少地更改您的代码。

Here is a working Code: 这是一个有效的代码:

import turtle

turtle.ht()

width = 800
height = 800
turtle.screensize(width, height)

##Definitions
def text(text, size, color, pos1, pos2):
     turtle.penup()
     turtle.goto(pos1, pos2)
     turtle.color(color)
     turtle.begin_fill()
     turtle.write(text, font=('Arial', size, 'normal'))
     turtle.end_fill()


def onTextClick(event):
    x, y = event.x, event.y
    print('x={}, y={}'.format(x, y))    
    if (x >= 600 and x <= 800) and (  y >= 280 and y <= 300):
        turtle.onscreenclick(lambda x, y: turtle.bgcolor('red'))

##Screen
turtle.bgcolor('purple')
text('This is an example', 20, 'orange', 100, 100)

canvas = turtle.getcanvas()
canvas.bind('<Motion>', onTextClick)    

turtle.done()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM