guomin shao
guomin shao

Reputation: 11

Tkinter by example

I'm learning Tkinter and am using the book Tkinter by example. I tried to test the example of the book, and something is wrong.

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

Error raised:

    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

Upvotes: 0

Views: 275

Answers (2)

Manjula Devi
Manjula Devi

Reputation: 149

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

This code will give you the desired output to add the items to the Tkinter list.

Worked with python 3.x

Upvotes: 1

Kevin
Kevin

Reputation: 76194

Is self.tasks a list, or a Tk object? If it's a list, then this code won't work because lists don't have a title() or geometry() method. If it's a Tk object, this code won't work because Tk objects don't have an append() method.

Try having separate arguments for the tasks list and the parent window object.

Additional miscellaneous bugs:

  • don't forget to pack() your Todo object
  • fix that file=tk.X typo in the for loop.
  • call bind on the Text rather than the frame
  • append new_task to self.tasks rather than tk
  • call delete on task_creat, not 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()

Result:

enter image description here

Upvotes: 1

Related Questions