编程语言
首页 > 编程语言> > python-在Tkinter中列出鼠标悬停事件函数

python-在Tkinter中列出鼠标悬停事件函数

作者:互联网

我正在将医疗工具的GUI制作为课程项目.给定条件后,它应输出从不同网站(如webMD)收集的一堆治疗选项.我希望能够处理任何列出的治疗方法的鼠标悬停事件,以提供有关该治疗方法的更多信息(例如,药物类别,是否为通用药物等).

标签存储在列表中,因为我不知道会预先返回多少种不同的处理方法.所以我的问题是如何使这些鼠标悬停事件起作用.我不能为每个可能的标签写一个函数定义,它们的数量可能成百上千.我敢肯定有一种非常Python的方式来做到这一点,但我不知道该怎么办.

这是我创建标签的代码:

    def search_click():
        """
        Builds the search results after the search button has been clicked
        """
        self.output_frame.destroy()                                                 # Delete old results
        build_output()                                                              # Rebuild output frames
        treament_list = mockUpScript.queryConditions(self.condition_entry.get())    # Get treatment data
        labels = []
        frames = [self.onceFrame, self.twiceFrame, self.threeFrame, self.fourFrame] # holds the list of frames
        for treament in treament_list:                                              # For each treatment in the list
            label = ttk.Label(frames[treament[1] - 1], text=treament[0])            # Build the label for treatment

            labels.append(label)                                                    # Add the treatment to the list
            label.pack()        

这就是GUI的样子(不要判断[-;)GUI image

文本“将鼠标悬停在药物上以获得信息”应根据鼠标悬停在哪种药物上进行更改.

解决方法:

I can’t write a function definition for every single possible label, they would number in the hundreds or thousands. I’m sure there’s a very pythonic way to do it, but I have no idea what.

签出lambda functions几乎与您想要的相同.

就您而言,类似:

def update_bottom_scroll_bar(text):
    # whatever you want to do to update the text at the bottom

for treatment in treament_list:  # For each treatment in the list
    label = ttk.Label(frames[treatment[1] - 1], text=treatment[0])  # Build the label for treatment

    label.bind("<Enter>", lambda event, t=treatment: update_bottom_scroll_bar(text=t))
    label.bind("<Leave>", lambda event: update_bottom_scroll_bar(text='Default label text'))

    labels.append(label)  # Add the treatment to the list
    label.pack()

还请正确拼写您的变量,我更正了治疗方法…

标签:mouseover,list,tkinter,python,user-interface
来源: https://codeday.me/bug/20191118/2025241.html