2013-01-22 28 views
0

我有麻煩的事件結合使用Python/Tkinter的工作。我只是試圖點擊並打印位置,但每次我這樣做時,結果都是「-1」。蟒蛇Tkinter的列表框的事件綁定

這裏是我的代碼

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('<Button-1>', 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() 

更新的代碼 - 我擺脫了框架,但我似乎無法找出爲什麼我在功能click_button越來越-1第一個print語句

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('<<ListboxSelect>>', 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() 
+1

你所說的打印「位置」是什麼意思?你想要項目的索引,x/y座標還是當前選中的數據? –

回答

1

listbox最近的項目是由y,不x發現。

self.nearest(event.x)  # wrong 
self.nearest(event.y)  # right 

更新:我沒有首先注意到真正的問題:

listbox = Listbox(frame) 

這不是你的子類相同的列表框,它的另一個不相關的列表框中。 列表框(這 make_list)是空的,這就是爲什麼它總是返回-1附近。

也許繼承框架是一個好主意(反正不是繼承列表框並添加另一個列表框框架爲更好)。然後你必須綁定事件real列表框不爲空。

快速的方法來看看它將固定在工作是如何調用一個真正的列表框的nearestevent.widget

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

仍顯示-1當我打印curIndex(在click_button方法) – user1104854

+0

@ user1104854解釋多個的一個問題。 –

+0

添加您建議的代碼顯示正確的位置。 Consriering我還是很新的,我確信我可以弄清楚這一點,但我想知道這種情況的最佳做法,所以我不會在刁鑽的代碼中遇到麻煩。你能給我一些關於你會改變什麼的建議嗎? – user1104854

6

在你問的最佳實踐的答案的評論。最佳做法是綁定到<<ListboxSelect>>,在列表框中選擇該項目後立即觸發。

This answer到類似的問題有一個例子。

+0

我更新了上面的編輯。我試着做你的建議,但是我得到0作爲輸出,介意看看? – user1104854

+0

@ user1104854:有兩個問題。一個是你要求self.nearest(),但self並不是擁有數據的小部件,而不是你點擊的小部件。你有一個非常奇怪的代碼,你有一個列表框中嵌入一個列表框。另外,我不'想''ListboxSelect >>'設置'event.x'和'event.y'。該事件不代表點擊,事件表示「當前項目已更改」,可能並不總是有x和y(即:如果使用鍵盤更改當前選擇)。 'event.widget.curselection()'是你應該使用的。 –