python-在Tkinter中列出鼠标悬停事件函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-在Tkinter中列出鼠标悬停事件函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2163字,纯文字阅读大概需要4分钟。
内容图文
![python-在Tkinter中列出鼠标悬停事件函数](/upload/InfoBanner/zyjiaocheng/666/c8c48a1b2f4a45a3914c6ebf67722585.jpg)
我正在将医疗工具的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的样子(不要判断[-;)
文本“将鼠标悬停在药物上以获得信息”应根据鼠标悬停在哪种药物上进行更改.
解决方法:
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()
还请正确拼写您的变量,我更正了治疗方法…
内容总结
以上是互联网集市为您收集整理的python-在Tkinter中列出鼠标悬停事件函数全部内容,希望文章能够帮你解决python-在Tkinter中列出鼠标悬停事件函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。