Tkinter列表框 [英] Tkinter Listbox

查看:155
本文介绍了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>>事件:

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列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆