2015-06-23 109 views
0

我試圖讓tkinter返回列表框中點擊的項目的索引。這是我的代碼。返回在tkinter列表框中點擊的項目的索引

def fileSelection(self): 
    selection = listbox.curselection 
    print(selection) 

listbox.bind("<Button-1>", fileSelection) 

現在它打印tkinter.Listbox對象的

綁定的方法Listbox.curselection在0x00320E30

無論是點擊什麼項目。如果我更改代碼,包括像這樣的按鈕:

button = Button(text=u"test", command=OnButtonClick) 

def OnButtonClick(): 
    selection = listbox.curselection() 
    print(selection) 

,並選擇列表框項,然後單擊該按鈕,將打印所選項目的指標,符合市場預期,但這是一個額外的步驟我不不想要。

回答

2
def fileSelection(self): 
    selection = listbox.curselection 
    print(selection) 

看起來你忘了括號。

def fileSelection(self): 
    selection = listbox.curselection() 
    print(selection) 
+0

基督......我可以刪除它嗎? * slinks away * – Volkisch

-1

根據effbot.org,輪詢小部件允許您執行點擊更新。

self.current = None 
self.listbox = Listbox(self) 
self.listbox.pack() 
self.poll() 

def poll(self): 
    now = self.listbox.curselection() 
    if now != self.current: 
     self.list_has_changed(now) 
     self.current = now 
    self.after(250, self.poll) 

def list_has_changed(self, selection): 
    print "selection is", selection 
+0

該effbot頁是_waaaayyyy_過期。當選擇改變時,GUI將激發一個事件('<< ListobxSelect >>')無論如何,你的解決方案並不回答被問到的實際問題,它只是解決它。 –

相關問題