Tkinter的例子

时间:2018-03-15 14:32:21

标签: python python-2.7 tkinter

我正在学习Tkinter并且正在使用这本书Tkinter by example。我试图测试这本书的例子,但是出了点问题。

import Tkinter as tk
class Todo(tk.Frame):
def __init__(self,tasks=None):
    tk.Frame.__init__(self,tasks)
    if not tasks:
        self.tasks=[]
    else:
        self.tasks=tasks
    self.tasks.title("To-Do App v1")
    self.tasks.geometry("300x400")
    todol=tk.Label(self,text="---Ado Items Here---",bg="lightgrey",fg="black",pady=10)
    self.tasks.append(todol)
    for task in self.tasks:
        task.pack(side=tk.TOP,file=tk.X)
    self.task_creat=tk.Text(self,height=3,bg="white",fg="black")
    self.task_creat.pack(side=tk.BOTTOM,fill=tk.X)
    self.task_creat.focus_set()
    self.bind("<Return>",self.add_task)
    self.colour_schemes=[{"bg":"lightgrey","fg":"black"},{"bg":"grey","fg":"white"}]
def add_task(self,event=None):
    task_text=self.task_creat.get(1.0,tk.END).strip()
    if len(task_text)>0:
        new_task = tk.Label(self,text=task_text,pady=10)
        _,task_style_choice=divmod(len(self.tasks),2)
        my_scheme_choice = self.colour_schemes[task_style_choice]
        new_task.configure(bg=my_scheme_choice["bg"])
        new_task.configure(fg=my_scheme_choice["fg"])
        new_task.pack(side=tk.TOP,fill=tk.X)
        tk.append(new_task)
    self.task_create.delete(1.0,tk.END)
if __name__=="__main__":
    todo=tk.Tk()
    app=Todo(todo)
    todo.mainloop()

引发错误:

    Traceback (most recent call last):

  File "<ipython-input-1-40cf89ea27bb>", line 1, in <module>
    runfile('E:/TKinter/tkinter_by_example/2_1.py', wdir='E:/TKinter/tkinter_by_example')

  File "D:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "D:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "E:/TKinter/tkinter_by_example/2_1.py", line 39, in <module>
    app=Todo(todo)

  File "E:/TKinter/tkinter_by_example/2_1.py", line 18, in __init__
    self.tasks.append(todol)

  File "D:\Anaconda\lib\lib-tk\Tkinter.py", line 1904, in __getattr__
    return getattr(self.tk, attr)

AttributeError: append

2 个答案:

答案 0 :(得分:1)

self.tasks是列表还是Tk对象?如果是列表,那么此代码将无效,因为列表没有title()geometry()方法。如果它是Tk对象,则此代码将不起作用,因为Tk对象没有append()方法。

尝试为tasks列表和父窗口对象设置单独的参数。

其他杂项错误:

  • 不要忘记你的Todo对象pack()
  • 修复了file=tk.X循环中的for拼写错误。
  • 在文字而不是框架
  • 上调用bind
  • new_task追加到self.tasks而不是tk
  • delete上致电task_creat,而不是task_create

import Tkinter as tk
class Todo(tk.Frame):
    def __init__(self,parent,tasks=None):
        tk.Frame.__init__(self,parent)
        if not tasks:
            self.tasks=[]
        else:
            self.tasks=tasks
        parent.title("To-Do App v1")
        parent.geometry("300x400")
        todol=tk.Label(self,text="---Ado Items Here---",bg="lightgrey",fg="black",pady=10)
        self.tasks.append(todol)
        for task in self.tasks:
            task.pack(side=tk.TOP,fill=tk.X)
        self.task_creat=tk.Text(self,height=3,bg="white",fg="black")
        self.task_creat.pack(side=tk.BOTTOM,fill=tk.X)
        self.task_creat.focus_set()
        self.task_creat.bind("<Return>",self.add_task)
        self.colour_schemes=[{"bg":"lightgrey","fg":"black"},{"bg":"grey","fg":"white"}]
    def add_task(self,event=None):
        task_text=self.task_creat.get(1.0,tk.END).strip()
        if len(task_text)>0:
            new_task = tk.Label(self,text=task_text,pady=10)
            _,task_style_choice=divmod(len(self.tasks),2)
            my_scheme_choice = self.colour_schemes[task_style_choice]
            new_task.configure(bg=my_scheme_choice["bg"])
            new_task.configure(fg=my_scheme_choice["fg"])
            new_task.pack(side=tk.TOP,fill=tk.X)
            self.tasks.append(new_task)
        self.task_creat.delete(1.0,tk.END)

if __name__=="__main__":
    todo=tk.Tk()
    app=Todo(todo)
    app.pack()
    todo.mainloop()

结果:

enter image description here

答案 1 :(得分:1)

import tkinter as tk
class Todo(tk.Tk):
def __init__(self, tasks=None):
    super().__init__()

    if not tasks:
        self.tasks = []
    else:
        self.tasks = tasks

    self.title("To-Do App v1")
    self.geometry("300x400")

    todo1 = tk.Label(self, text="--- Add Items Here ---", bg="lightgrey", fg="black", pady=10)

    self.tasks.append(todo1)

    for task in self.tasks:
        task.pack(side=tk.TOP, fill=tk.X)

    self.task_create = tk.Text(self, height=3, bg="white", fg="black")

    self.task_create.pack(side=tk.BOTTOM, fill=tk.X)
    self.task_create.focus_set()

    self.bind("<Return>", self.add_task)

    self.colour_schemes = [{"bg": "lightgrey", "fg": "black"}, {"bg": "grey", "fg": "white"}]

def add_task(self, event=None):
    task_text = self.task_create.get(1.0,tk.END).strip()

    if len(task_text) > 0:
        new_task = tk.Label(self, text=task_text, pady=10)

        _, task_style_choice = divmod(len(self.tasks), 2)

        my_scheme_choice = self.colour_schemes[task_style_choice]

        new_task.configure(bg=my_scheme_choice["bg"])
        new_task.configure(fg=my_scheme_choice["fg"])

        new_task.pack(side=tk.TOP, fill=tk.X)

        self.tasks.append(new_task)

    self.task_create.delete(1.0, tk.END)
if __name__ == "__main__":
    todo = Todo()
    todo.mainloop()

此代码将为您提供所需的输出,以将项目添加到Tkinter列表中。

  

使用python 3.x