Python GUI界面没有打开小部件?

时间:2013-11-28 00:51:06

标签: python tkinter

我正在尝试创建一个类似于待办事项列表的小部件:但我的程序似乎并不是出于某种原因而运行。它不会出错,但它不起作用..?

有人知道我的代码有什么问题吗?

from Tkinter import *
import tkFont

class App:

  def getTasks(self):
        return self.todo

  def getCompleted(self):
        return self.done

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

        self.master = master
        self.frame - Frame(master)
        self.frame.grid()

        self.saveButton = Button(self.frame, text="Save", command=self.save)
        self.saveButton.grid()
        self.restoreButton = Button(self.frame, text="Restore", command=self.res)
        self.restoreButton.grid(row=0, 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=0, column=4)

        frame1 = LabelFrame(self.frame, text="Tasks")
        frame1.grid(columnspam = 5)
        self.tasks = Listbox(frame1)
        self.task.grid()

        frame2=LabelFrame(self.frame, text="Completed")
        frame2.grid(columnspan=5)
        self.completed= Listbox(frame2)
        self.completed.grid()

  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 items:
              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)

有谁知道错误是什么?

1 个答案:

答案 0 :(得分:1)

您需要实际制作App的实例和Tkinter.Tk的实例:

...

if __name__ == '__main__':
    root = Tk()
    app = App(root)
    root.mainloop()

注意,我还没有尝试过其余的代码,所以我不知道它是否会起作用,但至少应该开始给你带来错误。