gpt4 book ai didi

python - 在 Tkinter 中制作鼠标悬停事件函数列表

转载 作者:行者123 更新时间:2023-11-28 16:21:20 27 4
gpt4 key购买 nike

我正在为一个医疗工具制作一个 GUI 作为一个类(class)项目。给定条件,它应该输出从不同网站(如 webMD)收集的一系列治疗方案。我希望能够处理列出的任何治疗上的鼠标悬停事件,以提供有关治疗的更多信息(例如药物类别,是否为泛型等)。

标签存储在一个列表中,因为我不知道事先会返回多少种不同的处理方法。所以我的问题是如何使这些鼠标悬停事件起作用。我无法为每个可能的标签编写函数定义,它们的数量可能有数百或数千。我确定有一种非常 pythonic 的方法可以做到这一点,但我不知道是什么。

这是我创建标签的代码:

    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()

另外请正确拼写你的变量,我将 treament 更正为 treatment...

关于python - 在 Tkinter 中制作鼠标悬停事件函数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40097711/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com