Unknown
Unknown

Reputation: 1

How would I change my functions into classes?

I'm working on a coursework for my uni (GUI program) and I run into a problem as my code works but the specification is that we use OOP instead of just functions, and I'm lost.

I tried making new classes for each button but I don't know how to make them work like they do in the functions.

def add():
    #get input
    task=txt_input.get()
    if task !="":
        tasks.append(task)
        #updating the list box
        update_listbox()
    else:
        display["text"]=("Input a task.")

    with open("ToDoList.txt", "a") as f:
        f.write(task)
        f.close()

txt_input=tk.Entry(root, width=25)
txt_input.pack(pady=15)

add=tk.Button(root, text="Add", fg="DarkOrchid3", bg="blanched almond", command=add)
add.pack(pady=5, ipadx=15)

This allows the user to add a task to his to-do list in the GUI, but like I said it should be using OOP not functions. If I get to understand this one, I should be able to do the rest of the buttons.

UPDATE: Ok so I tried the solution given below and I don't really know what is wrong with my code, the GUI appears but the adding functions won't work.

class ToDoList():
    def __init__(self):
        self.tasks = []

    def update_listbox(self):
        #calling clear function to clear the list to make sure tasks don't keep on adding up
        clear()
        for task in self.tasks:
            box_tasks.insert("end", task)

    def clear(self):
        box_tasks.insert("end", task)

class adding():

    def add(self):
        task=txt_input.get()
        if task!="":
            self.tasks.append(task)
            update_listbox()
        else:
            display["text"]=("Input a task")

Upvotes: 0

Views: 56

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

It's not clear what your teacher meant about using classes. I'm going to guess that they want you to create a "todo list" object that has methods for adding and removing tasks. I don't know whether they want the GUI to be part of that class or not. It could be that your entire application is made of classes, or you could only use the class for the business logic.

I think you should start by creating a class just for the business logic. It would look something like this:

class ToDoList():
    def __init__(self):
        self.the_list = []
    def add(self, value):
        <code to add the value to self.the_list>
    def remove(self, item):
        <code to remove a value from self.the_list>

With that, you can write a simple little program without a GUI, which makes it easy to test the logic:

# create an instance of the to-do list
todo_list = ToDoList() 

# add two items:
todo_list.add("mow the lawn")
todo_list.add("buy groceries")

# delete the first item:
todo_list.remove(0)

To build a GUI on top of that, you could either add the GUI component to the existing class, or create a new class specifically for the GUI. Each has pros and cons.

In the following example, the GUI is a separate class which uses the ToDoList class to maintain the data. This design lets you re-use the underlying todo list logic in tests, in a GUI, and even in a webapp or possibly even a mobile app.

class ToDoGUI():
    def __init__(self):
        # initalize the list
        self.todo_list = ToDoList()

        # initialize the GUI
        <code to create the entry, button, and widget to show the list>

    def add(self):
        # this should be called by your button to the list
        data = self.entry.get()
        self.todo_list.add(data)

Upvotes: 1

Related Questions