【问题标题】:How To Fix "TypeError: 'NoneType' object does not support item assignment"如何修复“TypeError:‘NoneType’对象不支持项目分配”
【发布时间】:2020-10-30 08:58:25
【问题描述】:

我试图创建一个登录窗口,但是当我按下窗口中的提交按钮而不是附加到文件中的值时,它显示TypeError: 'NoneType' object does not support item assignment 我还希望当我按下提交按钮时,谢谢窗口会打开,但它会在我启动程序时立即打开。

我的代码

counter=1
from tkinter import *
import openpyxl


wb=openpyxl.Workbook()
sheet=wb.active
sheet["A1"]="Name"
sheet["B1"]="Guardian Name"
sheet["C1"]="DOB"
sheet["D1"]="Age"    
sheet["E1"]="Address"
sheet["F1"]="Contact Number"
wb.save(filename="Login.xlsx")
def submitvalue():
    global counter
    wb=openpyxl.Workbook("Login.xlsx")
    sheet=wb.active
    sheet[f"A{counter+1}"]=name.get()
    sheet[f"B{counter+1}"]=guardian.get()
    sheet[f"C{counter+1}"]=Dob.get()
    sheet[f"D{counter+1}"]=Age.get()
    sheet[f"F{counter+1}"]=Address.get()
    sheet[f"G{counter+1}"]=contactnumber.get()
    counter+=1
    thankwin.mainloop()    
root=Tk()
root.geometry("444x555")
# Heading
Head=Frame(root)
Head.pack(fill="x")
Label(Head,text="Login Form",fg="white",bg="red",font="k 23 bold italic underline").pack(fill="x")

# Login fixed text
main=Frame(root)
main.pack()
Label(main,text="Name",font="arial 15",justify="left").grid(row=0,column=0)
Label(main,text="Guardian Name",font="arial 15",justify="left").grid(row=1,column=0)
Label(main,text="Date of Birth",font="arial 15",justify="left").grid(row=2,column=0)
Label(main,text="Age",font="arial 15",justify="left").grid(row=3,column=0)
Label(main,text="Address",font="arial 15",justify="left").grid(row=4,column=0)
Label(main,text="Contact Number",font="arial 15",justify="left").grid(row=5,column=0)


# Creating entry widgets
name=StringVar(root)
guardian=StringVar(root)
Dob=StringVar(root)
Age=IntVar(root)
Address=StringVar(root)
contactnumber=IntVar(root)


Entry(main,textvariable=name).grid(row=0,column=1)
Entry(main,textvariable=guardian).grid(row=1,column=1)
Entry(main,textvariable=Dob).grid(row=2,column=1)
Entry(main,textvariable=Age).grid(row=3,column=1)
Entry(main,textvariable=Address).grid(row=4,column=1)
Entry(main,textvariable=contactnumber).grid(row=5,column=1)
Button(main,text="Submit",command=submitvalue).grid(row=6,column=0)

# Footer
Footer=Frame(root)
Footer.pack(side="bottom",fill="x")
Label(Footer,text="Contact Number:",fg="blue").pack()
# Thanku window
thankwin=Tk()
Label(thankwin,text="Thank You").pack()
Button(thankwin,text="OK",command=exit).pack()
root.mainloop()

错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__      
    return self.func(*args)
  File "c:\Users\Tanmay\Documents\Programming\Tkinter\exercise.py", line 76, in submitvalue
    sheet[f"A{counter+1}"]=name.get()
TypeError: 'NoneType' object does not support item assignment

【问题讨论】:

  • sheet 设置为 None,因此您无法使用项目分配语法 (None[...] = ...) 分配任何内容。
  • 那我该怎么办?我是初学者。
  • 问题出在submitvalue。打开"Login.xlsx"后,wb.active就是None。所以不要尝试使用它。您可能应该在尝试访问之前添加对wb == None(或wb is None)的检查。
  • 你试过把条目和相应的网格放在不同的行吗?

标签: python python-3.x exception tkinter nonetype


【解决方案1】:

将提交值中的 wb=openpyxl.Workbook("Login.xlsx") 替换为 openpyxl.load_workbook("Login.xlsx")

【讨论】:

  • 它没有显示错误但数据也没有附加到文件中。
  • @TanmayDaga 是因为你在工作表中插入数据后没有调用wb.save("Login.xlsx")
猜你喜欢
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 2016-07-31
  • 2014-02-18
  • 2017-03-26
相关资源
最近更新 更多