簡體   English   中英

如何修復“TypeError: 'NoneType' object 不支持項目分配”

[英]How To Fix “TypeError: 'NoneType' object does not support item assignment”

I tried to make a login window but when I press submit button in window instead of the values getting appended in file its shows TypeError: 'NoneType' object does not support item assignment And I also want that when I press submit button the thankyou window opens但它會在我啟動程序后立即打開。

我的代碼

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

在 submitvalue 中用 openpyxl.load_workbook("Login.xlsx") 替換 wb=openpyxl.Workbook("Login.xlsx")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM