我收到一个 AttributeError: 'NoneType' 对象没有属性 'get' 对于我

我收到 ButtonClick 方法内任何函数的属性错误。请帮助纠正这个问题。


import tkinter as tk

from tkinter import *


root = tk.Tk()


enter = tk.Entry(root, width = 35, borderwidth = 5).grid(row = 0, column = 0, columnspan = 3, padx =10 , pady = 10)


def ButtonClick(number):


    print(number)

    current = enter.get()

    print(current)

    # enter.delete(0,END)

    # enter.insert(0, current + number)



button1 = tk.Button(root, text="1", bg="yellow",padx = 40, pady= 20, command=lambda: ButtonClick(1)).grid(row = 1, column = 0)

root.mainloop()


杨__羊羊
浏览 103回答 1
1回答

森栏

python shell 使实验变得很容易。假设错误是因为enter我刚刚将代码的第一部分粘贴到 shell 中。>>> import tkinter as tk>>> from tkinter import *>>> >>> root = tk.Tk()>>> >>> enter = tk.Entry(root, width = 35, borderwidth = 5).grid(row = 0, column = 0, columnspan = 3, padx =10 , pady = 10)>>> repr(enter)'None'是的,它的None。这是典型的。对象方法通常不返回自己的对象。它对于方法链接很方便,但大多数对象并不是为此而设计的。只需按照示例和教程所示操作即可,并将其放在单独的行中。import tkinter as tkfrom tkinter import *root = tk.Tk()enter = tk.Entry(root, width = 35, borderwidth = 5)enter.grid(row = 0, column = 0, columnspan = 3, padx =10 , pady = 10)def ButtonClick(number):    print(number)    current = enter.get()    print(current)    # enter.delete(0,END)    # enter.insert(0, current + number)button1 = tk.Button(root, text="1", bg="yellow",padx = 40, pady= 20, command=lambda: ButtonClick(1)).grid(row = 1, column = 0)root.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python