Python Tkinter类继承问题
我想操纵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没有属性...”
我之前发布了一些内容我意外地运行了旧版本的代码,所以我引用了错误的错误。对不起,如果你看到那篇文章。现在走了
在我看来,没有理由的行动是在一个类...
#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()
我打算在课堂上添加更多的方法。我只是试图让它工作在第一位。 – user1104854
@ user1104854 - 没关系 - 但我仍然没有看到他们在课堂上有任何理由 - 而且绝对不是在从已有的任何课程中继承的课程中。 – mgilson
为什么你建议不要让他们上课? (我了解不再继承) – user1104854
在我看来,你在这里有一个循环导入... – mgilson
@mgilson我从SortActions文件中删除了tkinter导入,如果这就是你正在谈论的。 – user1104854
这段代码有点混乱。 'Actions'是一个'MakeList',它是一个'Tkinter.Listbox',它是一个小部件。由于'Actions'最终是一个小部件,它需要被构建为一个小部件 - 'Widget(master,...)',但这不是你如何实例化它......就我所见,它没有动作'从'MakeList'继承的原因(或者甚至是一个类的事情......) – mgilson