python tkinter listbox,python tkinter listbox事件绑定

I'm having trouble getting an event binding to work with python/tkinter. I'm simply trying to click and have the location printed, but every time I do this, "-1" is the result.

Here's my code

from Tkinter import *

import Tkinter

class make_list(Tkinter.Listbox):

def __init__(self,master, **kw):

frame = Frame(master)

frame.pack()

self.build_main_window(frame)

kw['selectmode'] = Tkinter.SINGLE

Tkinter.Listbox.__init__(self, master, kw)

master.bind('', self.click_button)

master.curIndex = None

#display the clicked location

def click_button(self, event):

self.curIndex = self.nearest(event.x)

print self.curIndex

#display the window, calls the listbox

def build_main_window(self, frame):

self.build_listbox(frame)

#listbox

def build_listbox(self, frame):

listbox = Listbox(frame)

for item in ["one", "two", "three", "four"]:

listbox.insert(END, item)

listbox.insert(END, "a list entry")

listbox.pack()

return

if __name__ == '__main__':

tk = Tkinter.Tk()

make_list(tk)

tk.mainloop()

updated code - I got rid of frame but I can't seem to figure out why I'm getting -1 for the first print statement in the function click_button

from Tkinter import *

import Tkinter

class make_list(Tkinter.Listbox):

#display the clicked location

def click_button(self, event):

##this block works

w = event.widget

index = int(w.curselection()[0])

value = w.get(index)

print value

##this doesn't

self.curIndex = self.nearest(event.y)

print self.curIndex

self.curIndex = event.widget.nearest(event.y)

print self.curIndex

#display the window, calls the listbox

def build_main_window(self):

self.build_listbox()

#listbox

def build_listbox(self):

listbox = Listbox()

listbox.bind('<>', self.click_button)

for item in ["one", "two", "three", "four"]:

listbox.insert(END, item)

listbox.insert(END, "a list entry")

listbox.pack()

return

if __name__ == '__main__':

tk = Tkinter.Tk()

start = make_list(tk)

start.build_main_window()

start.mainloop()

解决方案

listbox nearest item is found by y, not x.

self.nearest(event.x) # wrong

self.nearest(event.y) # right

Update: I didn't notice the real problem first:

listbox = Listbox(frame)

It's not the same listbox which you subclassed, it's another unrelated listbox. Your listbox (which is make_list) is empty, that's why it always returns -1 for nearest.

Perhaps subclassing a frame is a good idea (anyway, better than subclassing listbox and adding a frame with another listbox into it). Then you'll have to bind event on that real listbox which is not empty.

Quick way to see how it will work when fixed is to call nearest of a real listbox with event.widget:

self.curIndex = event.widget.nearest(event.y)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值