3

大部分代码是由我的教授提供的,我必须更改他的格式。但是,当我运行程序时,他的原始代码甚至没有在 tk 框中提供任何内容。我做了一些改变,但没有什么是很有效的。有什么建议吗??

这是我的主要代码:

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()

todo进口是:

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
4

2 回答 2

2

您还没有创建该类的实例todoApp

在此处添加此行:

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

a = todoApp(master)

master.mainloop()

不过,您确实需要弄乱定位,当我运行它时,它看起来有点乱。

编辑:

这正是你要找的吗?

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()
于 2012-11-23T08:50:12.717 回答
1

在我看来,您还没有创建类的实例。

在文件的底部,添加以下内容:

myApp = todoApp(master)
master.mainloop()

可能它们必须以相反的顺序排列(我正在工作,所以我现在无法尝试)。

于 2012-11-23T08:47:23.913 回答