如何在 Tkinter 中创建响应单击事件的透明矩形 [英] how to create a transparent rectangle responding to click event in Tkinter

查看:23
本文介绍了如何在 Tkinter 中创建响应单击事件的透明矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 tkinter.canvas 中绘制一个矩形来响应点击事件:

I need to draw a rectangle in a tkinter.canvas to respond click event:

click_area = self.canvas.create_rectangle(0,0,pa_width,pa_height,fill='LightBlue',outline='lightBlue',tags=['A','CLICK_AREA'])
self.canvas.tag_bind('CLICK_AREA','<Button>',self.onClickArea)

它有效.

此时,我必须在画布上绘制一系列网格,并且我希望它们被click_area覆盖,因此我需要制作click_area透明.

at this moment, I have to draw a series of grid on the canvas, and I want them to be covered with the click_area, so that I need to make the click_area transparent.

但是,当我这样写时:

click_area = self.canvas.create_rectangle(0,0,pa_width,pa_height,fill='',outline='lightBlue',tags=['A','CLICK_AREA'])

它不再没有响应点击.

所以,我的问题是如何使其透明并保持对点击的响应.或者,有没有其他方法可以实现我想要的.

So, my question is how to make it transparent and keep it responding to click. Or, is there any other way to implement what I want.

非常感谢.

推荐答案

我想我明白了:绑定画布,而不是矩形.

I think I got it: Bind the canvas, not the rectangle.

替换

self.canvas.tag_bind('CLICK_AREA','<Button>',self.onClickArea)

self.canvas.bind('<Button>',self.onClickArea)

问题解决了.

这篇关于如何在 Tkinter 中创建响应单击事件的透明矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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