pythontkinter:Listbox鼠标输入特定条目的事件

pythontkinter:Listbox鼠标输入特定条目的事件,python,tkinter,listbox,mouseover,Python,Tkinter,Listbox,Mouseover,可以使用/,为鼠标指针进入/离开整个列表框时创建事件。如何跟踪鼠标何时进入或离开列表框中的特定输入行 我想用不同的颜色来显示鼠标指针当前所在条目的背景 否,您无法跟踪它何时进入/离开特定行。但是,您可以跟踪它何时进入/离开小部件,并且可以使用列表框的索引方法计算鼠标在哪个项目上。如果你给出一个@x,y形式的索引,它将返回数值索引 例如: self.listbox.bind("<Enter>", self.on_enter) ... def on_enter(self, event):

可以使用/,为鼠标指针进入/离开整个列表框时创建事件。如何跟踪鼠标何时进入或离开列表框中的特定输入行


我想用不同的颜色来显示鼠标指针当前所在条目的背景

否,您无法跟踪它何时进入/离开特定行。但是,您可以跟踪它何时进入/离开小部件,并且可以使用列表框的索引方法计算鼠标在哪个项目上。如果你给出一个@x,y形式的索引,它将返回数值索引

例如:

self.listbox.bind("<Enter>", self.on_enter)
...
def on_enter(self, event):
    index = self.listbox.index("@%s,%s" % (event.x, event.y))
    ...

否,您无法跟踪它何时进入/离开特定行。但是,您可以跟踪它何时进入/离开小部件,并且可以使用列表框的索引方法计算鼠标在哪个项目上。如果你给出一个@x,y形式的索引,它将返回数值索引

例如:

self.listbox.bind("<Enter>", self.on_enter)
...
def on_enter(self, event):
    index = self.listbox.index("@%s,%s" % (event.x, event.y))
    ...

下面是通过绑定到事件而不是配对和来完成所需操作的一半尝试。这是因为只有当我们从列表框外部进入列表框时才会引发,但一旦我们使用鼠标进入列表框,则不会引发其他事件,并且我们无法跟踪鼠标位于哪个项目上方

每次鼠标移动时调用一个函数可能会导致超负荷工作,所以我认为这种功能不值得这样做

程序仍然不能很好地工作,我仍然必须理解为什么:基本上,有时候项目的背景和字体颜色没有正确地改变,有某种延迟或其他原因

from tkinter import *


class CustomListBox(Listbox):

    def __init__(self, master=None, *args, **kwargs):
        Listbox.__init__(self, master, *args, **kwargs)

        self.bg = "white"
        self.fg = "black"
        self.h_bg = "#eee8aa"
        self.h_fg = "blue"

        self.current = -1  # current highlighted item

        self.fill()

        self.bind("<Motion>", self.on_motion)
        self.bind("<Leave>", self.on_leave)

    def fill(self, number=15):
        """Fills the listbox with some numbers"""
        for i in range(number):
            self.insert(END, i)
            self.itemconfig(i, {"bg": self.bg})
            self.itemconfig(i, {"fg": self.fg})

    def reset_colors(self):
        """Resets the colors of the items"""
        for item in self.get(0, END):
            self.itemconfig(item, {"bg": self.bg})
            self.itemconfig(item, {"fg": self.fg})

    def set_highlighted_item(self, index):
        """Set the item at index with the highlighted colors"""
        self.itemconfig(index, {"bg": self.h_bg})
        self.itemconfig(index, {"fg": self.h_fg})    

    def on_motion(self, event):
        """Calls everytime there's a motion of the mouse"""
        print(self.current)
        index = self.index("@%s,%s" % (event.x, event.y))
        if self.current != -1 and self.current != index:
            self.reset_colors()
            self.set_highlighted_item(index)
        elif self.current == -1:
            self.set_highlighted_item(index)
        self.current = index

    def on_leave(self, event):
        self.reset_colors()
        self.current = -1


if __name__ == "__main__":
    root = Tk()
    CustomListBox(root).pack()
    root.mainloop()

请注意,我使用了from tkinter import*以加快键入速度,但我建议您使用import tkinter as tk。

这里有一个半尝试,通过绑定到事件而不是成对和来完成您想要的任务。这是因为只有当我们从列表框外部进入列表框时才会引发,但一旦我们使用鼠标进入列表框,则不会引发其他事件,并且我们无法跟踪鼠标位于哪个项目上方

每次鼠标移动时调用一个函数可能会导致超负荷工作,所以我认为这种功能不值得这样做

程序仍然不能很好地工作,我仍然必须理解为什么:基本上,有时候项目的背景和字体颜色没有正确地改变,有某种延迟或其他原因

from tkinter import *


class CustomListBox(Listbox):

    def __init__(self, master=None, *args, **kwargs):
        Listbox.__init__(self, master, *args, **kwargs)

        self.bg = "white"
        self.fg = "black"
        self.h_bg = "#eee8aa"
        self.h_fg = "blue"

        self.current = -1  # current highlighted item

        self.fill()

        self.bind("<Motion>", self.on_motion)
        self.bind("<Leave>", self.on_leave)

    def fill(self, number=15):
        """Fills the listbox with some numbers"""
        for i in range(number):
            self.insert(END, i)
            self.itemconfig(i, {"bg": self.bg})
            self.itemconfig(i, {"fg": self.fg})

    def reset_colors(self):
        """Resets the colors of the items"""
        for item in self.get(0, END):
            self.itemconfig(item, {"bg": self.bg})
            self.itemconfig(item, {"fg": self.fg})

    def set_highlighted_item(self, index):
        """Set the item at index with the highlighted colors"""
        self.itemconfig(index, {"bg": self.h_bg})
        self.itemconfig(index, {"fg": self.h_fg})    

    def on_motion(self, event):
        """Calls everytime there's a motion of the mouse"""
        print(self.current)
        index = self.index("@%s,%s" % (event.x, event.y))
        if self.current != -1 and self.current != index:
            self.reset_colors()
            self.set_highlighted_item(index)
        elif self.current == -1:
            self.set_highlighted_item(index)
        self.current = index

    def on_leave(self, event):
        self.reset_colors()
        self.current = -1


if __name__ == "__main__":
    root = Tk()
    CustomListBox(root).pack()
    root.mainloop()
请注意,我使用了from tkinter import*以加快键入速度,但我建议您使用import tkinter作为tk