Tkinter帆布假期事件未被触发

时间:2016-01-31 08:39:05

标签: python canvas tkinter

我会在Leave事件中填充矩形橙色,但是当我在矩形上留下以在其他矩形中输入时,有时候离开事件不会被触发。

from tkinter import *
from functools import partial

ENTER = '7'
LEAVE = '8'

def change_color_on_enter_or_leave(event):
    canvas = event.widget
    item_ids = canvas.find_closest(event.x, event.y)
    for item_id in item_ids:
        if event.type == ENTER:
            canvas.itemconfig(item_id, fill="orange")
        elif event.type == LEAVE:
            canvas.itemconfig(item_id, fill="red")

root = Tk()
canvas = Canvas(root, background="white")
label = Label(root)

for x in range(0, 200, 50):
    for y in range(0, 200, 50):
        canvas.create_rectangle((x, y), (x + 40, y + 40), fill='red', tag='rectangle')

canvas.tag_bind('rectangle', '<Enter>', change_color_on_enter_or_leave)
canvas.tag_bind('rectangle', '<Leave>', change_color_on_enter_or_leave)

canvas.grid()
label.grid()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以为每个矩形分配唯一标记,然后使用lambda将此标记发送到函数

from tkinter import *
from functools import partial

ENTER = '7'
LEAVE = '8'

# extra argument - unique tag
def change_color_on_enter_or_leave(event, tag):

    canvas = event.widget

    # find by tag
    item_ids = canvas.find_withtag(tag)

    for item_id in item_ids:
        if event.type == ENTER:
            canvas.itemconfig(item_id, fill="orange")
        elif event.type == LEAVE:
            canvas.itemconfig(item_id, fill="red")



root = Tk()
canvas = Canvas(root, background="white")
label = Label(root)

for x in range(0, 200, 50):
    for y in range(0, 200, 50):

        # create unique tag
        tag = 'rectangle_%s_%s' % (x,y)
        canvas.create_rectangle((x, y), (x + 40, y + 40), fill='red', tag=tag)

        # use unique tag in function
        canvas.tag_bind(tag, '<Enter>', lambda event,t=tag:change_color_on_enter_or_leave(event,t))
        canvas.tag_bind(tag, '<Leave>', lambda event,t=tag:change_color_on_enter_or_leave(event,t))

canvas.grid()
label.grid()

root.mainloop()

编辑: create_rectangle()会返回唯一ID,因此您可以使用它而不是唯一标记。

from tkinter import *
from functools import partial

ENTER = '7'
LEAVE = '8'

# extra argument - rectangle ID
def change_color_on_enter_or_leave(event, r_id):

    canvas = event.widget

    # you don't have to search IDs

    if event.type == ENTER:
        canvas.itemconfig(r_id, fill="orange")
    elif event.type == LEAVE:
        canvas.itemconfig(r_id, fill="red")

root = Tk()
canvas = Canvas(root, background="white")
label = Label(root)

for x in range(0, 200, 50):
    for y in range(0, 200, 50):

        # get rectangle ID
        r_id = canvas.create_rectangle((x, y), (x + 40, y + 40), fill='red', tag='rectangle') # tag "rectangle" use for something different

        # use rectangle ID with function
        canvas.tag_bind(r_id, '<Enter>', lambda event,t=r_id:change_color_on_enter_or_leave(event,t))
        canvas.tag_bind(r_id, '<Leave>', lambda event,t=r_id:change_color_on_enter_or_leave(event,t))

canvas.grid()
label.grid()

root.mainloop()