首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python类继承问题

Python类继承问题
EN

Stack Overflow用户
提问于 2013-01-25 14:29:22
回答 1查看 1.4K关注 0票数 0

我在试图操纵Tkinter的一个列表箱,但我遇到了一些麻烦。我以前把所有的东西都放在一节课上,放在一页纸上,一切都很好。我在两个不同的页面上将方法分成不同的类(一个用于显示东西,一个用于修改它们),现在我遇到了一些问题。

我得到了下面的错误AttributeError: Actions没有属性'listbox'。我假设这与继承有关,因为在我将它分成两个文件之前,它运行得很好。

这是第一个文件

代码语言:javascript
运行
复制
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()

还有第二个文件,那个我有问题的文件

代码语言:javascript
运行
复制
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实例没有.“上面说"MakeList没有属性.“

我之前发布了一些东西,但是我意外地运行了一个旧版本的代码,所以我引用了错误的错误。很抱歉你看到那条柱子了。它现在消失了

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-25 14:38:25

在我看来,行动没有理由出现在课堂上.

代码语言:javascript
运行
复制
#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

现在您可以使用以下操作:

代码语言:javascript
运行
复制
   ...
   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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14523822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档