python - 'NoneType'对象在python小程序中没有属性'get'
问题描述
此代码运行但当我按下它创建的按钮时崩溃。这是我的第一篇文章,所以如果您有提示或需要更多信息,请发表评论。程序应该将盒子的变量保存在全局变量中。但是当我尝试按下按钮时发生错误。
from tkinter import *
finestra1 = Tk()
finestra1.title("Prima Finestra")
testo1 = Label(finestra1, text ="Inserire modello infissi").grid(row=0, column=0)
spazioinput1 = Entry(finestra1, width=10, borderwidth=5).grid(row=0, column=1)
testo2= Label(finestra1, text ="Inserire numero finestre").grid(row=1, column=0)
spazioinput2 = Entry(finestra1, width=10, borderwidth=5).grid(row=1, column=1)
testo3= Label(finestra1, text ="Inserire numero balconi").grid(row=2, column=0)
spazioinput3 = Entry(finestra1, width=10, borderwidth=5).grid(row=2, column=1)
def primobottone():
# global modelloinfissi
global numerofinestre
global numerobalconi
modelloinfissi = spazioinput1.get()
numerofinestre = int(spazioinput3.get())
numerobalconi = int(spazioinput2.get())
Label(finestra1, text="Modello: " +modelloinfissi +" \nnumero: "+ numerobalconi+numerofinestre)
bottone1 = Button(finestra1, text= "Avanti", command = primobottone).grid(row=3)
finestra1.mainloop()
我遇到的错误:
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/salvatorefroncillo/Desktop/progetto/progetto.py", line 18, in primobottone
modelloinfissi = spazioinput1.get()
AttributeError: 'NoneType' object has no attribute 'get'
解决方案
Entry.grid()
返回None
,所以在这一行之后:
spazioinput1 = Entry(finestra1, width=10, borderwidth=5).grid(row=0, column=1)
你spazioinput1
确实是None
。您需要将其拆分为两个语句:
spazioinput1 = Entry(finestra1, width=10, borderwidth=5)
spazioinput1.grid(row=0, column=1)
当然,对你所有的小部件也做同样的事情......
推荐阅读
- arrays - 检查元组数组(二维数组)在 Typescript 中是否至少有 1 项
- pycharm - 如何将 str 转换为列表并划分(拆分)单词,例如列表的分隔元素?
- java - JavaEE Servlet Oracle 连接
- json - 在 JSON 中存储非敏感数据
- android - 单个远程内部的 Git 本地子模块
- delphi - Delphi TTaskDialog 位置打开
- apache-flink - 基于多个 KeySelector 对 Flink DataSet 进行排序
- python - 如何解决 tkinter.tclError python
- javascript - 没有得到正确的平方根返回
- sql - from 语句附近的 SQL 转换问题