'noneType'对象在tkinter上没有属性'get'错误 [英] 'NoneType' object has no attribute 'get' error on tkinter
问题描述
我最近开始使用 tkinter
在python 3.6中进行编码,并尝试在 repl.it
上创建自己的项目.该项目是一个简单的交互式待办事项,但是我被困住了,无法使用该功能.该函数只是简单地获取条目并将其添加到列表框中,但是当我尝试添加该函数时,它将返回
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'
这是代码:
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?
推荐答案
该错误表明以下行上的对象实例已返回 None
而不是 Entry
:
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()
因此
tasks = ti.get()
失败,因为 ti
的类型为 None
,而不是类型为 Entry
.
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()
这篇关于'noneType'对象在tkinter上没有属性'get'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!