Tkinter Listbox(Tkinter 列表框)
问题描述
我想一键执行功能列表框.这是我的想法:
I want to execute function with one click on listbox. This is my idea:
from Tkinter import *
import Tkinter
def immediately():
print Lb1.curselection()
top = Tk()
Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")
Lb1.pack()
Lb1.bind('<Button-1>', lambda event :immediately() )
top.mainloop()
但是这个函数在执行选择之前打印...当你运行这个代码时你会看到问题是什么.
But this function print before execute selecting...You will see what is the problrm when you run this code.
推荐答案
您可以绑定到 <<ListboxSelect>>
事件,如本文所述:Tkinter 列表框选择更改时获取回调?TKinter 有点奇怪,因为信息似乎不包含在发送给处理程序的事件中.还要注意,不需要创建一个简单地调用你的函数立即
的lambda,函数对象可以传入,如下所示:
You can bind to the <<ListboxSelect>>
event as described in this post: Getting a callback when a Tkinter Listbox selection is changed?
TKinter is somewhat strange in that the information does not seemed to be contained within the event that is sent to the handler. Also note, there is no need to create a lambda that simply invokes your function immediately
, the function object can be passed in as shown:
from Tkinter import *
import Tkinter
def immediately(e):
print Lb1.curselection()
top = Tk()
Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")
Lb1.pack()
Lb1.bind('<<ListboxSelect>>', immediately)
top.mainloop()
这篇关于Tkinter 列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Tkinter 列表框


- YouTube API v3 返回截断的观看记录 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01