如何在tkinter中的画布项上实现鼠标悬停回调?

时间:2016-08-03 08:56:02

标签: python tkinter tkinter-canvas mousehover

我使用下面的代码在互联网上找到了在python中实现鼠标悬停动作:

from tkinter import *
import numpy as np

class rect:
    def __init__(self, root):
        self.root = root
        self.size = IntVar()
        self.canvas = Canvas(self.root, width=800, height=300)
        self.scale = Scale(self.root, orient=HORIZONTAL, from_=3, to=20, tickinterval=1, variable=self.size)
        self.scale.bind('<ButtonRelease>', self.show)
        self.canvas.bind('<Motion>', self.motion)
        self.board = []
        self.array = np.zeros((self.scale.get(),self.scale.get())).tolist()
        self.canvas.pack()
        self.scale.pack()

    def motion(self,event):
        if self.canvas.find_withtag(CURRENT):
            current_color = self.canvas.itemcget(CURRENT, 'fill')
            self.canvas.itemconfig(CURRENT, fill="cyan")
            self.canvas.update_idletasks()
            self.canvas.after(150)
            self.canvas.itemconfig(CURRENT, fill=current_color)

    def show(self,event):
        self.canvas.delete('all')
        x = 50
        y = 50
        row = []
        self.board.clear()
        for i in range(self.scale.get()):
            row = []
            for j in range(self.scale.get()):
                rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red')
                x += 50
                row.append(rectangle)
            x -= j*50
            y +=50
            self.board.append(row)
        print(self.board)

root = Tk()
a = rect(root)
root.mainloop()

执行的问题是项目的颜色仅在有限的时间内变为蓝色。

每当我进入其区域时,我都需要更改画布中每个项目的颜色,并保持蓝色直到鼠标离开该项目。

2 个答案:

答案 0 :(得分:3)

您可以在创建矩形时传递参数activefill

来自effboot.org

  

填充要在鼠标指针移动到项目上时使用的颜色,如果   与填充不同。

为此,请替换:

rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red')

人:

rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red', activefill='cyan')

这消除了将Motion绑定到画布的需要,并且还使代码显着缩短:

from tkinter import *
import numpy as np

class rect:
    def __init__(self, root):
        self.root = root
        self.size = IntVar()
        self.canvas = Canvas(self.root, width=800, height=300)
        self.scale = Scale(self.root, orient=HORIZONTAL, from_=3, to=20, tickinterval=1, variable=self.size)
        self.scale.bind('<ButtonRelease>', self.show)
        self.board = []
        self.array = np.zeros((self.scale.get(),self.scale.get())).tolist()
        self.canvas.pack()
        self.scale.pack()

    def show(self,event):
        self.canvas.delete('all')
        x = 50
        y = 50
        row = []
        self.board.clear()
        for i in range(self.scale.get()):
            row = []
            for j in range(self.scale.get()):
                rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red', activefill='cyan')
                x += 50
                row.append(rectangle)
            x -= j*50
            y +=50
            self.board.append(row)
        print(self.board)

root = Tk()
a = rect(root)
root.mainloop()

答案 1 :(得分:0)

我更改了motion方法并将self.last = None添加到__init__方法:

from tkinter import *
import numpy as np

class rect:
    def __init__(self, root):
        self.root = root
        self.size = IntVar()
        self.canvas = Canvas(self.root, width=800, height=300)
        self.scale = Scale(self.root, orient=HORIZONTAL, from_=3, to=20, tickinterval=1, variable=self.size)
        self.scale.bind('<ButtonRelease>', self.show)
        self.canvas.bind('<Motion>', self.motion)
        self.board = []
        self.array = np.zeros((self.scale.get(),self.scale.get())).tolist()
        self.canvas.pack()
        self.scale.pack()
        self.last = None

    def motion(self, event):
        temp = self.canvas.find_withtag(CURRENT)
        if temp == self.last:
            self.canvas.itemconfig(CURRENT, fill="cyan")
            self.canvas.update_idletasks()
        else:
            self.canvas.itemconfig(self.last, fill="red")
        self.last = temp

    def show(self,event):
        self.canvas.delete('all')
        x = 50
        y = 50
        row = []
        self.board.clear()
        for i in range(self.scale.get()):
            row = []
            for j in range(self.scale.get()):
                rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red')
                x += 50
                row.append(rectangle)
            x -= j*50
            y +=50
            self.board.append(row)
        print(self.board)

root = Tk()
a = rect(root)
root.mainloop()