如何在 Tkinter 中创建响应单击事件的透明矩形

编程入门 行业动态 更新时间:2024-10-27 20:26:24
本文介绍了如何在 Tkinter 中创建响应单击事件的透明矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我需要在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 08:01:23,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩形   单击   透明   事件   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!