AttributeError: 'NoneType' 对象没有属性 'delete' [英] AttributeError: 'NoneType' object has no attribute 'delete'

查看:58
本文介绍了AttributeError: 'NoneType' 对象没有属性 'delete'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题,但我不明白为什么.

I have run into this issue and I can't understand why.

我从我的应用程序中提取了我的代码并制作了这个测试代码,这样您就不必通过一堆垃圾来了解我的要求.

I took my code from my application and made this test code so you don't have to go through a bunch of junk to see what I am asking.

我有这个在其他代码中工作.但是在比较了两者之后,我终生无法弄清楚这一点.

I have this working in other code. But after comparing the two, I can't for the life of me figure this out.

在这个应用程序中,我收到错误AttributeError: 'NoneType' object has no attribute 'delete'".

In this application, I get the error "AttributeError: 'NoneType' object has no attribute 'delete' ".

import Tkinter as tk

def main():
    mainWindow = tk.Tk()
    v = tk.StringVar()
    entryBox = tk.Entry(mainWindow, textvariable=v).grid(column=0, row=1)
    def test():
        entryBox.delete(0,20)
    testButton = tk.Button(mainWindow, text='Go!', command=test, padx=10).grid(row=2, column=0) 
    tk.mainloop()
main()

推荐答案

在这一行:

entryBox = tk.Entry(mainWindow, textvariable=v).grid(column=0, row=1)

grid 不返回任何内容,因此 entryBox 是 None,它没有删除方法.您必须将 entryBox 设置为 tk.Entry(mainWindow, textvariable=v) 然后在 entryBox 上调用 grid 方法>

grid doesn't return anything, so entryBox is None, which doesn't have a delete method. You have to set entryBox to tk.Entry(mainWindow, textvariable=v) then call the grid method on entryBox

这篇关于AttributeError: 'NoneType' 对象没有属性 'delete'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆