Python Tkinter类继承问题

时间:2013-01-25 14:29:22

标签: python oop class inheritance tkinter

我试图操纵Tkinter中的列表框,但我遇到了一些麻烦。我曾经在一个页面上拥有所有内容,并且工作正常。我在两个不同的页面上将方法分成不同的类(一个用于显示事物,一个用于修改它们),现在我遇到了一些问题。

我收到以下错误属性错误:操作没有属性' 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(一个对象),但没有出现错误说&#34;动作实例没有....&#34;它说&#34; MakeList没有属性...&#34;

我之前发布了一些内容,但我不小心运行了旧版本的代码,因此我引用了错误的错误。对不起,如果你看到那个帖子。它现在已经消失了

1 个答案:

答案 0 :(得分: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()