Python Tkinter 类继承问题

0 投票
1 回答
1399 浏览
提问于 2025-04-17 13:39

我正在尝试在Tkinter中操作一个列表框,但遇到了一些问题。我之前把所有东西都放在一个类里,一个页面上,运行得很好。后来我把方法分成了两个不同的类,分别在两个页面上(一个用来显示内容,一个用来修改内容),现在就出现了一些问题。

我遇到了一个错误,提示AttributeError: Actions没有属性'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(一个对象),但错误信息变成了“MakeList没有属性...”而不是“Actions实例没有...”

我之前发过一个帖子,但我不小心运行了旧版本的代码,所以我引用了错误的信息。如果你看到了那个帖子,我很抱歉。现在它已经删除了。

1 个回答

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()

撰写回答