Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a blank Tkinter box when typing it this code in Python?

Tags:

python

tkinter

Most of this code was provided by my professor, and I have to make changes to his format. However, his original code is not even providing ANYTHING in the tk box when I run the program. I made some changes, but nothing is quite working. Any advice??

This is my main code:

from Tkinter import *
import todo
master = Tk()

class todoApp():

    def __init__(self, master):
        self.master = master
        self.frame = Frame(master)
        self.frame.grid()
        self.todo = todo.todoList()

        self.saveButton = Button(self.frame, text="Save", command=self.save)
        self.saveButton.grid()
        self.restoreButton = Button(self.frame, text="Restore", command = self.restore)
        self.restoreButton.grid(row=1, column=1)
        self.addButton = Button(self.frame, text="Add", command=self.add)
        self.addButton.grid(row=0,column=2)
        self.doneButton = Button(self.frame, text="Done", command=self.done)
        self.doneButton.grid(row=0,column=3)
        self.button = Button(self.frame, text="Quit", command=self.quit)
        self.button.grid(row=0, column=4)

        label = Label(self.frame, text="New Task: ")
        label.grid()
        self.entry = Entry(self.frame)
        self.entry.grid(row=1, column=1, columnspan=4)

        frame1 = LabelFrame(self.frame, text="Tasks")
        frame1.grid(columnspan=5, sticky=E+W)
        frame1.columnconfigure(0,weight=1)
        self.tasks = Listbox(frame1)
        self.tasks.grid(sticky=E+W)

        frame2 = LabelFrame(self.frame, text="Completed")
        frame2.grid(columnspan=5, sticky=E+W)
        frame2.columnconfigure(0,weight=1)
        self.completed = Listbox(frame2)
        self.completed.grid(sticky=E+W)


    def save(self):
        self.todo.saveList("tasks.txt")

    def restore(self):
        self.todo.restoreList("tasks.txt")
        items = self.todo.getTasks()
        self.tasks.delete(0,END)
        for item in [ "study", "grocery shop", "cram for finals!", "sleep", "build a gingerbread house" ]:
            self.tasks.insert(END,item)
        items = self.todo.getCompleted()
        self.completed.delete(0,END)
        for item in items:
            self.completed.insert(END,item)

    def add(self):
        task = self.entry.get()
        self.todo.addTask(task)
        self.tasks.insert(END,task)

    def done(self):
        selection = self.tasks.curselection()
        if len(selection) == 0:
            return
        task = self.tasks.get(selection[0])
        self.todo.completeTask(task)
        self.tasks.delete(selction[0])
        self.completed.insert(END,task)

    def quit(self):
        self.frame.quit()
        self.master.destroy()

master.mainloop()

The todo import is:

class todoList:
    def __init__(self):
        self.todo = []
        self.done = []

    def addTask(self,task):
        self.todo.append(task)

    def completeTask(self,task):
        if self.todo.count(task) > 0:
           self.todo.remove(task)
           self.done.append(task)

    def getTasks(self):
        return self.todo

    def getCompleted(self):
        return self.done
like image 240
Megan Clumpus Avatar asked May 24 '26 18:05

Megan Clumpus


2 Answers

You havent created an instance of the class todoApp,

add this line here:

    def quit(self):
    self.frame.quit()
    self.master.destroy()

a = todoApp(master)

master.mainloop()

You do need to mess around with the positioning though, when I ran it, it looked a bit messy.

edit:

is this exactly what you're looking for?

from Tkinter import *
import todo
master = Tk()

class todoApp():

    def __init__(self, master):
        self.master = master
        self.frame = Frame(master)
        self.frame.grid()
        self.todo = todo.todoList()

        self.buttonframe = LabelFrame(self.frame, text='controls')
        self.buttonframe.grid(row=0, column=0, columnspan=2)
        self.saveButton = Button(self.buttonframe, text="Save", command=self.save)
        self.saveButton.grid()
        self.restoreButton = Button(self.buttonframe, text="Restore", command = self.restore)
        self.restoreButton.grid(row=0, column=1)
        self.addButton = Button(self.buttonframe, text="Add", command=self.add)
        self.addButton.grid(row=0,column=2)
        self.doneButton = Button(self.buttonframe, text="Done", command=self.done)
        self.doneButton.grid(row=0,column=3)
        self.button = Button(self.buttonframe, text="Quit", command=self.quit)
        self.button.grid(row=0, column=4)

        label = Label(self.frame, text="New Task: ")
        label.grid(row=1, column=0)
        self.entry = Entry(self.frame)
        self.entry.grid(row=1, column=1)

        frame1 = LabelFrame(self.frame, text="Tasks")
        frame1.grid(columnspan=5, sticky=E+W)
        frame1.columnconfigure(0,weight=1)
        self.tasks = Listbox(frame1)
        self.tasks.grid(sticky=E+W)

        frame2 = LabelFrame(self.frame, text="Completed")
        frame2.grid(columnspan=5, sticky=E+W)
        frame2.columnconfigure(0,weight=1)
        self.completed = Listbox(frame2)
        self.completed.grid(sticky=E+W)


    def save(self):
        self.todo.saveList("tasks.txt")

    def restore(self):
        self.todo.restoreList("tasks.txt")
        items = self.todo.getTasks()
        self.tasks.delete(0,END)
        for item in [ "study", "grocery shop", "cram for finals!", "sleep", "build a gingerbread house" ]:
            self.tasks.insert(END,item)
        items = self.todo.getCompleted()
        self.completed.delete(0,END)
        for item in items:
            self.completed.insert(END,item)

    def add(self):
        task = self.entry.get()
        self.todo.addTask(task)
        self.tasks.insert(END,task)

    def done(self):
        selection = self.tasks.curselection()
        if len(selection) == 0:
            return
        task = self.tasks.get(selection[0])
        self.todo.completeTask(task)
        self.tasks.delete(selection[0])
        self.completed.insert(END,task)

    def quit(self):
        self.frame.quit()
        self.master.destroy()

a = todoApp(master)

master.mainloop()
like image 182
jbaldwin Avatar answered May 26 '26 07:05

jbaldwin


It seems to me like you haven't created an instance of your class.

In the bottom of your file, add this:

myApp = todoApp(master)
master.mainloop()

It could be that they must be in reverse order (I'm at work so I can't try it out right now).

like image 32
Karaaie Avatar answered May 26 '26 06:05

Karaaie