问题
I recently started coding in python 3.6 with tkinter
and tried creating my own project on repl.it
. The project is a simple interactive to-do list, however I am stuck and cant get the function to work. The function is just simply getting the entry and adding it to a list box, but when I try adding the function it returns
'NoneType' object has no attribute 'get'
Here is the code:
from tkinter import *
root = Tk()
#giving the window its colour
root.configure(bg = '#40e0d0')
#changing the size of the window
root.geometry('200x500')
#creating an empty List
tasks = []
#creating the functions
def uplistbox():
# clearing the current list
clear_listbox()
# populating the Listbox
for task in tasks:
lb.insert('end', task)
def addingtask():
#getting the task
tasks = ti.get()
#adding the task to the Listbox
tasks.append(task)
#updating the listbox
uplistbox()
#creating the title and adding the display below it
lbl1 = Label(root, text='To-Do-List', bg = 'red').pack()
display = Label(root, text='', bg = 'yellow').pack()
ti = Entry(root, width = 15).pack()
# adding buttons
btn1 = Button(root, text = 'Add task', bg = 'yellow', command = addingtask).pack()
btn2 = Button(root, text = 'Delete All', bg = 'yellow').pack()
btn3 = Button(root, text = 'Delete', bg = 'yellow').pack()
btn4 = Button(root, text = 'Sort(asc)', bg = 'yellow').pack()
btn5 = Button(root, text = 'Sort(desc)', bg = 'yellow').pack()
btn6 = Button(root, text = 'Choose random', bg = 'yellow').pack()
btn7 = Button(root, text = 'Number of tasks', bg = 'yellow').pack()
btn8 = Button(root, text = 'exit', bg = 'yellow').pack()
lb = Listbox(root, bg = 'white').pack()
root.mainloop()
Can anyone tell me what I'm doing wrong?
回答1:
The error indicates that the object instantiation on the following line has returned None
instead of an instance of Entry
:
ti = Entry(root, width = 15).pack()
Therefore
tasks = ti.get()
fails because ti
is of type None
instead of type Entry
.
The following should do the trick:
def addingtask():
# getting the task
ti = Entry(root, width = 15)
ti.pack()
tasks = ti.get()
# adding the task to the Listbox
tasks.append(task)
# updating the listbox
uplistbox()
回答2:
You should replace the following:
ti = Entry(root, width = 15).pack()
To:
ti = Entry(root, width = 15)
ti.pack()
Because Entry.pack
returns nothing, which assigns your ti
to None
.
来源:https://stackoverflow.com/questions/59898281/nonetype-object-has-no-attribute-get-error-on-tkinter