2013-01-25 144 views
0

我想操縱Tkinter中的列表框,但我遇到了一些麻煩。我曾經在一個課堂上擁有所有的東西,在一個頁面上,它運行良好。我將這些方法分成兩個不同頁面上的不同類(一個用於顯示內容,一個用於修改它們),現在我遇到了一些問題。Python Tkinter類繼承問題

我收到以下錯誤AttributeError:操作沒有屬性'listbox'。我假設它是繼承相關的東西,因爲它在我將它分成兩個文件之前工作正常。

這裏的第一個文件

from Tkinter import * 
import Tkinter 
import SortActions 

class MakeList(Tkinter.Listbox): 

    def BuildMainWindow(self): 
     menubar = Frame(relief=RAISED,borderwidth=1) 
     menubar.pack() 

     mb_file = Menubutton(menubar,text='file') 
     mb_file.menu = Menu(mb_file) 
     mb_file.menu.add_command(label='open', command = self.BuildListbox) 
     mb_file.pack(side=LEFT) 

     mb_edit = Menubutton(menubar,text='edit') 
     mb_edit.menu = Menu(mb_edit) 
     mb_edit.pack(padx=25,side=RIGHT) 

     mb_file['menu'] = mb_file.menu 
     mb_edit['menu'] = mb_edit.menu 
     return 

    def BuildListbox(self): 
     self.listbox = Tkinter.Listbox() 
     index = SortActions.Actions() 
     self.listbox.bind('<<ListboxSelect>>', index.GetWindowIndex) 
     MoveItem = SortActions.Actions() 
     self.listbox.bind('<B1-Motion>', index.MoveWindowItem) 
     for item in ["one", "two", "three", "four"]: 
      self.listbox.insert(END, item)  
     self.listbox.insert(END, "a list entry") 
     self.listbox.pack() 
     #print self.listbox.get(0, END) 
     return 

if __name__ == '__main__': 
    start = MakeList() 
    start.BuildMainWindow() 
    mainloop() 

第二個文件,即我在與

from FileSort import MakeList 


class Actions(MakeList): 

    #gets the current item that was clicked in the window 
    def GetWindowIndex(self, event): 
     w = event.widget 
     self.curIndex = int(w.curselection()[0]) 

    #moves the current item in the window when clicked/dragged 
    def MoveWindowItem(self, event): 
     i = self.listbox.nearest(event.y) #here is where the error is occurring 
     print i 

我以爲,因爲我繼承MakeList類我應該有準入問題之一。我也嘗試改變它,所以我直接訪問MakeList(一個對象),而不是錯誤說「Actions instance has no ....」它說「MakeList沒有屬性...」

我之前發佈了一些內容我意外地運行了舊版本的代碼,所以我引用了錯誤的錯誤。對不起,如果你看到那篇文章。現在走了

+0

在我看來,你在這裏有一個循環導入... – mgilson

+0

@mgilson我從SortActions文件中刪除了tkinter導入,如果這就是你正在談論的。 – user1104854

+0

這段代碼有點混亂。 'Actions'是一個'MakeList',它是一個'Tkinter.Listbox',它是一個小部件。由於'Actions'最終是一個小部件,它需要被構建爲一個小部件 - 'Widget(master,...)',但這不是你如何實例化它......就我所見,它沒有動作'從'MakeList'繼承的原因(或者甚至是一個類的事情......) – mgilson

回答

1

在我看來,沒有理由的行動是在一個類...

#SortActions.py 

#gets the current item that was clicked in the window 
def GetWindowIndex(self, event): 
    w = event.widget 
    self.curIndex = int(w.curselection()[0]) 

#moves the current item in the window when clicked/dragged 
def MoveWindowItem(self, event): 
    i = self.nearest(event.y) #here is where the error is occurring 
    print i 

現在你可以使用動作:

... 
    def BuildListbox(self): 
     #self.listbox = Tkinter.Listbox() #??? This has no master widget ... 
     #Since this is already a listbox, there's no point in building another ... 

     self.bind('<<ListboxSelect>>', lambda e:SortActions.GetWindowIndex(self,e)) 

     self.bind('<B1-Motion>', lambda e:SortActions.MoveWindowItem(self,e) 
     for item in ("one", "two", "three", "four"): 
      self.insert(END, item)  
     self.insert(END, "a list entry") 
     self.pack() 
+0

我打算在課堂上添加更多的方法。我只是試圖讓它工作在第一位。 – user1104854

+0

@ user1104854 - 沒關係 - 但我仍然沒有看到他們在課堂上有任何理由 - 而且絕對不是在從已有的任何課程中繼承的課程中。 – mgilson

+0

爲什麼你建議不要讓他們上課? (我瞭解不再繼承) – user1104854