我發佈了一個問題here,我被建議重構代碼。現在已經不同了,我覺得有必要提出一個新的問題。Python/Tkinter列表框麻煩
無論如何,我想添加拖放功能的列表框,但我想好的第一步將是事件綁定工作在第一位。目前,當我點擊列表時,單擊列表框後出現以下錯誤。列表框窗口出現,但是當我點擊它時發生錯誤。
"AttributeError: make_list instance has no attribute 'nearest'.
另外,當我在build_listbox方法中打印listbox時,會打印下面的小數點.40720520L。不應該打印列表框中的值嗎?最後,它是在相同的方法。列表框不正確創建?
from Tkinter import *
import Tkinter
class make_list:
def move_mouse(self, event):
self.curIndex = event.nearest(event.y)
print self.curIndex
def click_button(self, event):
w= event.widget
self.curIndex = int(w.curselection()[0])
#print self.curIndex
value = w.get(self.curIndex)
print value
def build_main_window(self):
self.build_listbox()
def build_listbox(self):
listbox = Listbox()
listbox.bind('<<ListboxSelect>>', self.click_button)
listbox.bind('<B1-Motion>', self.move_mouse)
for item in ["one", "two", "three", "four"]:
listbox.insert(END, item)
listbox.insert(END, "a list entry")
listbox.pack()
print listbox
return
if __name__ == '__main__':
start = make_list()
start.build_main_window()
mainloop()
你可以發佈完整的回溯,以便我們知道哪一行產生異常? – mgilson
實際上,你在'move_mouse()'定義的第一行得到一個AttributeError:Event實例沒有'nearest'的屬性。這是因爲'event'對象沒有'nearest'方法函數屬性。 – martineau
@martineau,我該如何改變?我很抱歉,我對tkinter和python一般都很陌生 – user1104854