So, I have got the following code in my second(return) window:
Why do I get the above error in returnMovie: index = self.selected_index.get()
I know that the returnMovie function isn't working, I am just curious why I get that error?
def returnMovie(self):
returningAccount = controll1.GetAccount(self.Input.get())
return_dialog = MyReturnWindow(self, returningAccount)
self.wait_window(return_dialog)
class MyReturnWindow(Toplevel):
def __init__(self, parent, Account):
super().__init__(parent)
self.second_frame = Frame(self)
self.second_frame.pack(fill=BOTH, expand=True)
self.myReturnButton = Button(self, text='Return', command=self.returnMovie(Account))
self.myReturnButton.pack(side=LEFT)
self.selected_index = IntVar(self, -1)
self.item_source = Variable(self, Account.get_movies())
self.item_source.set(Account.get_movies())
self.listbox_return = Listbox(self.second_frame, listvariable=self.item_source)
self.listbox_return.bind("<<ListboxSelect>>", self.listbox_return_selection_changed)
self.listbox_return.pack(fill=BOTH, expand=True)
def listbox_return_selection_changed(self, event):
index = self.listbox_return.curselection()[0]
self.selected_index.set(index)
self.selected_movie.set(controll1.GetMovies()[index])
def returnMovie(self, Account):
index = self.selected_index.get()
movie = self.selected_movie.get()
if index in range(0, self.listbox_return.size()):
controll1.GetAccountMovies(Account).pop(index)
controll1.GetMovies().pop(index)
self.item_source.set(controll1.GetAccountMovies(Account))
else:
messagebox.showerror('No Movie found', 'There was no movie found')
You get the error because of this line of code:
self.myReturnButton = Button(self, text='Return', command=self.returnMovie(Account))
The above code is exactly the same as this:
result = self.returnMovie(Account)
self.myReturnButton = Button(self, text='Return', command=result)
Thus, you are calling self.returnMovie(Account)
before setting self.selected_index
, so it throws an error when it tries to reference that attribute.
When setting the command
option you must provide a callable -- essentially the name of a function. Normally you should provide a command that doesn't require an argument, but since your command requires an argument then a simple solution is to use lambda
:
self.myReturnButton = Button(..., command=lambda: self.returnMovie(Account))