简体   繁体   English

Tkinter绑定到弧

[英]Tkinter bind to arc

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. 我正在使用Tkinter在python中编写一个简单的-and-crosss游戏,并希望仅在将鼠标悬停在它们上方时显示Os。 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. 为此,我知道我需要添加一个事件绑定,但是不知道如何,因为当我创建一个圆(弧)时,我没有得到一个返回的对象,而是一个ID号。 How can I use this id number to create an event binding? 如何使用该ID号创建事件绑定?

Instead of using the ID number to create an event binding, I would recommend using a tag system to go about this. 我建议您不要使用ID号来创建事件绑定,而应该使用标签系统来解决此问题。 When creating your oval object, eg canvas.create_oval(100, 100, 200, 200) , add ,tag="tag_name" inside the parenthesis to apply a tag to your created object. 创建椭圆形对象(例如canvas.create_oval(100, 100, 200, 200) ,请在圆括号内添加,tag="tag_name"以将标签应用于创建的对象。 You can then bind to this tag explicitly, for example using the tag_bind function of the canvas. 然后,您可以显式绑定到此标签,例如使用画布的tag_bind函数。

You could create something like this, after creating an oval with tag "oval": 在创建带有标签“ oval”的椭圆后,您可以创建如下所示的内容:

canvas.tag_bind("oval", "<ButtonPress-1>", 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). 然后,仅当用户单击您给标签“ oval”(并传递事件)的对象时,此函数才调用您的函数pressed_oval() )。

Hopefully this helps you get started! 希望这可以帮助您入门!

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="" ). 我要注意, tag_bind正常工作,该对象仍必须在画布上,因此与其将椭圆的状态更改为隐藏或打包而忘记它们,只需将椭圆的填充在颜色和空白之间切换( fill="" )。

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

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