Python 如何在Tkinter中创建响应单击事件的透明矩形
我需要在tkinter.canvas中绘制一个矩形以响应单击事件:Python 如何在Tkinter中创建响应单击事件的透明矩形,python,tkinter,Python,Tkinter,我需要在tkinter.canvas中绘制一个矩形以响应单击事件: 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 = 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)
它不再对单击做出响应
所以,我的问题是如何使它透明,并保持它对点击的响应。或者,有没有其他方法来实现我想要的
非常感谢。我想我明白了:绑定画布,而不是矩形 替换
self.canvas.tag_bind('CLICK_AREA','<Button>',self.onClickArea)
self.canvas.tag\u bind('CLICK\u区域','',self.onclick区域)
与
self.canvas.bind(“”,self.onClickArea)
问题已解决。我在尝试使用
find\u closest
Canvas方法修改现有矩形时遇到了同样的问题,但简单地绑定到Canvas不起作用。问题是,没有填充的Tkinter矩形只会对其边框上的单击作出响应
然后,我从以下内容中了解了创建矩形的点画参数:
点画:指示矩形内部显示方式的位图
被点画
默认值为点画=”,表示纯色。A.
典型值为点画='gray25'。除非填充,否则无效
已设置为某种颜色
位图部分指出,默认情况下只有少数点画选项可用,但没有一个是完全透明的。但是,您可以将自己的自定义位图指定为X位图图像(一个.xbm
文件)
XBM文件实际上只是具有类似C语法的文本文件,因此我用所有透明像素制作了自己的2x2位图,并将其保存为transparent.XBM
,与我的Tkinter脚本位于同一目录中。以下是XBM文件的代码:
#define trans_width 2
#define trans_height 2
static unsigned char trans_bits[] = {
0x00, 0x00
};
然后,在创建矩形时,可以通过在xbm
文件名前加@
前缀来指定自定义点画:
self.canvas.create_rectangle(
x1,
y1,
x2,
y2,
outline='green',
fill='gray', # still needed or stipple won't work
stipple='@transparent.xbm',
width=2
)
注意,您仍然需要提供一些填充值,否则点画将不会应用。实际填充值无关紧要,因为点画将在画布中“覆盖”它。非常感谢。我一直在寻找一个透明的填充解决方案有一段时间了
#define trans_width 2
#define trans_height 2
static unsigned char trans_bits[] = {
0x00, 0x00
};
self.canvas.create_rectangle(
x1,
y1,
x2,
y2,
outline='green',
fill='gray', # still needed or stipple won't work
stipple='@transparent.xbm',
width=2
)