Tkinter绑定到弧 [英] Tkinter bind to arc

查看:83
本文介绍了Tkinter绑定到弧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的方法-和-使用Tkinter在python中进行交叉游戏,并且希望仅在将鼠标悬停在它们上方时显示Os.为此,我知道我需要添加一个事件绑定,但是不知道如何,因为当我创建一个圆(弧)时,我没有得到一个返回的对象,而是一个ID号.如何使用此ID号创建事件绑定?

I'm writing a simple noughts - and - crosses game in python using Tkinter and want to have the Os show only when you hover over them. To do this, I know I need to add an event binding, but don't know how because when I create a circle (arc) I don't get an object returned but an id number instead. How can I use this id number to create an event binding?

推荐答案

我建议不要使用ID号来创建事件绑定,而建议使用标签系统来解决此问题.创建椭圆形对象时,例如canvas.create_oval(100, 100, 200, 200),在括号内添加,tag="tag_name",以将标签应用于您创建的对象.然后,您可以显式绑定到此标签,例如使用画布的tag_bind功能.

Instead of using the ID number to create an event binding, I would recommend using a tag system to go about this. When creating your oval object, e.g. canvas.create_oval(100, 100, 200, 200), add ,tag="tag_name" inside the parenthesis to apply a tag to your created object. You can then bind to this tag explicitly, for example using the tag_bind function of the canvas.

在创建带有标签"oval"的椭圆形之后,您可以创建类似这样的内容:

You could create something like this, after creating an oval with tag "oval":

canvas.tag_bind("oval", "<ButtonPress-1>", pressed_oval)

然后,只有当用户单击您给标签"oval"(并传递事件)的对象时,此函数才调用名为pressed_oval()的函数.

This would then call your function called pressed_oval() only when the user clicks on the object you gave the tag "oval" (and passing event to it).

希望这可以帮助您入门!

Hopefully this helps you get started!

我会注意到,要使tag_bind正常工作,该对象仍必须在画布上,因此与其将椭圆的状态更改为隐藏或忘记它们,不如将椭圆的填充颜色和颜色之间切换即可(fill="").

I would note that for tag_bind to work, the object still has to be on the canvas, so instead of changing the state of your ovals to hidden or pack forgetting them, just switch the ovals fill between your colour and nothing (fill="").

这篇关于Tkinter绑定到弧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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