Reputation: 1
I'm working with OOP, and I ran into a problem where when I call the method inside a class by using tkinter command it gives me an error.
I've tried different ways of calling the method but I'm stuck.
class ToDoList():
def __init__(self):
self.tasks = []
def update_listbox(self):
self.clear()
for task in self.tasks:
box_tasks.insert("end", task)
def clear(self):
box_tasks.insert("end", task)
def add(self):
task=txt_input.get()
if task !=" ":
tasks.append(task)
self.update_listbox()
else:
display["text"]=("Input a task")
tkinter command call:
add=tk.Button(root, text="Add", fg="DarkOrchid3", bg="blanched almond", command=ToDoList.add)
add.pack(pady=5, ipadx=15)
txt_input=tk.Entry(root, width=25)
txt_input.pack(pady=15)
error:
TypeError: add() missing 1 required positional argument: 'self'
This is the error, I understand that it should be defined but I don't really know what it means by it...
Upvotes: 0
Views: 73
Reputation: 7268
Try by creating object for class ToDoList()
. As class functions can be accessed from object of that class.
add=tk.Button(root, text="Add", fg="DarkOrchid3", bg="blanched almond", command=ToDoList().add)
Upvotes: 0
Reputation: 59114
add
is an instance method and you don't instantiate ToDoList
. If you make an instance of ToDoList
, you can pass that instance's .add
method.
todo = ToDoList()
add = tk.Button(root, text="Add", fg="DarkOrchid3", bg="blanched almond", command=todo.add)
Upvotes: 1