Reputation: 2167
I'm trying to manipulate a listbox in Tkinter but I'm having some troubles. I used to have everything in one class, on one page, and it worked fine. I separated the methods into different classes on two different pages (one for displaying things, one for modifying them) and now I'm having some issues.
I'm getting the following error AttributeError: Actions has no attribute 'listbox'. I'm assuming it's something inheritance related because it worked fine before I split it into two files.
Here's the first file
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()
And the second file, the one that I'm having issues with
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
I assumed since I inherit the MakeList class I should have access. I also tried changing it so I directly accessed MakeList (an object) but instead of the error saying "Actions instance has no...." it said "MakeList has no attribute..."
I posted something previously but I accidentally ran an older version of the code, so I was referencing the wrong error. Sorry if you saw that post. It's gone now
Upvotes: 0
Views: 1413
Reputation: 309899
As I see it, there's no reason for the Actions to be in a class ...
#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
Now you can use the actions:
...
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()
Upvotes: 1