TypeError:delete()缺少1个必需的位置参数:“indexPosition”

2024-04-20 11:57:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个简单的客户注册程序,当我单击一个名为“已保存”的按钮时,会弹出一个表,使用一个列表进行更新。我还制作了一个delete函数,通过鼠标点击删除选中的行,我将该函数绑定到一个回调函数,该函数将在文本字段中设置文本(cNamecEmailbirthdate)。我的问题是,每当我单击delete按钮时,它都会显示一个错误--TypeError:delete()缺少一个必需的位置参数:“indexPosition”我尝试使用不同的传递方法从一个函数传递到另一个函数,但我仍然感到困惑

我的代码:

from tkinter import *
from tkinter import messagebox
import tkinter as tk

at = False
dotcom = False
list = [['ID', 'NAME', 'EMAIL', 'BIRTHDATE']]
ID = 1
storedIndex = 0


def callback(event):


    custid.config(state=NORMAL)
    custid.delete('1.0', END)
    custid.insert(1.0, list[event.widget._values][0])
    custid.config(state=DISABLED)

    cName.set(list[event.widget._values][1])
    cEmail.set(list[event.widget._values][2])
    birthdate.set(list[event.widget._values][3])

    indexPosition = event.widget._values
    delete(indexPosition)

def createTable():
    for i in range(len(list)):
        for j in range(len(list[0])):
            mgrid = tk.Entry(window, width = 10, bg= 'yellow')
            mgrid.insert(tk.END, list[i][j])
            mgrid._values= i
            mgrid.grid (row = i + 5,column = j + 5)
            mgrid.bind("<Button-1>", callback)


def delete(indexPosition):
   list.pop(indexPosition)
   createTable()


def save ():
    customerName = False
    Email = False
    birthday = False
    emailCheck = False
    finalCheck = False

    monthCheck = False
    dayCheck = False
    yearCheck = False
    minorCheck = False
    slash = False

    monthCheck31 = False
    monthCheck30 = False
    monthCheck28 = False

    global at
    global dotcom
    global ID


    if len(custName.get()) != 0:
        labelNameCheck.config(text="First Name Last Name")
        customerName = True
    else:
        labelNameCheck.config(text="!")

    if len(custEmail.get()) != 0:
        labelEmail.config(text="[a-z]@[a-z].com")
        Email = True
    else:
        labelEmail.config(text="!")

    if len(custBday.get()) != 0:
        birthdateCheck.config(text="mm/dd/yyyy")
        birthday = True
    else:
        birthdateCheck.config(text="!")

    for x in range(len(custBday.get())):


        if x == 9:
            # Slash Checking
            if custBday.get()[2] == "/" and custBday.get()[5] == "/":
                slash = True

            # Month Checking
            if (int(custBday.get()[0:2]) <= 12):
                monthCheck = True

            # Day of Month Checking
            if (int(custBday.get()[0:2]) == 1):
                monthCheck31 = True

            if (int(custBday.get()[0:2]) == 2):
                monthCheck28 = True

            if (int(custBday.get()[0:2]) == 3):
                monthCheck31 = True

            if (int(custBday.get()[0:2]) == 4):
                monthCheck30 = True

            if (int(custBday.get()[0:2]) == 5):
                monthCheck31 = True

            if (int(custBday.get()[0:2]) == 6):
                monthCheck30 = True

            if (int(custBday.get()[0:2]) == 7):
                monthCheck31 = True

            if (int(custBday.get()[0:2]) == 8):
                monthCheck31 = True

            if (int(custBday.get()[0:2]) == 9):
                monthCheck30 = True

            if (int(custBday.get()[0:2]) == 10):
                monthCheck31 = True

            if (int(custBday.get()[0:2]) == 11):
                monthCheck30 = True

            if (int(custBday.get()[0:2]) == 12):
                monthCheck31 = True


            # Day Checking
            if monthCheck31 == True and int(custBday.get()[3:5]) <= 31 and int(custBday.get()[0:2]) != 0:
                dayCheck = True

            elif monthCheck30 == True and int(custBday.get()[3:5]) <= 30 and int(custBday.get()[0:2]) != 0:
                dayCheck = True

            elif monthCheck28 == True and int(custBday.get()[3:5]) <= 28 and int(custBday.get()[0:2]) != 0:
                dayCheck = True



            # Minor Checking
            if int(custBday.get()[6:8]) == 20 and int(custBday.get()[8:10]) <= 2:
                minorCheck = True

            if int(custBday.get()[6:8]) == 19 and int(custBday.get()[8:10] <= 99):
                minorCheck = True

            # Year Checking
            if minorCheck == True and int(custBday.get()[6:8]) != 0:
                yearCheck = True

            if int(custBday.get()[6:8]) > 20:
                yearCheck = False

    emailInfo = custEmail.get()


    if '@' in emailInfo:
        at = True

    else:
        at = False

    if '.com' in emailInfo:
        dotcom = True
    else:
        dotcom = False

    if at == True and dotcom == True:
            emailCheck = True

    if monthCheck == True and dayCheck == True and yearCheck == True and slash == True:
        finalCheck = True

    if customerName == True and Email == True and birthday == True and emailCheck == True and finalCheck == True:
        custid.config(state= NORMAL)
        initialValue = int(custid.get('1.0', END))
        custid.delete('1.0', END)
        list.append([ID, custName.get(), custEmail.get(), custBday.get()])
        custid.insert(1.0, initialValue + 1)
        custid.config(state=DISABLED)
        print(list)
        createTable()

        msgbox("Saved", "Record")


        ID = ID + 1
    else:
        msgbox("Incorrect Info Provided", "Record")



def keyup(e):

   global at
   global dotcom
   for x in range(len(custEmail.get())):
        if custEmail.get()[-1] == "@" and x > 1:
            at = True
        else:
            labelEmail.config(text="Bad Email")

        if x > 4 and custEmail.get()[-4:] == ".com" and at == True:
            dotcom = True
        else:
            labelEmail.config(text="Bad Email")

        if at == True and dotcom == True:
            labelEmail.config(text="Good Email")

        else:
            labelEmail.config(text="Bad Email")

def keyupBdate(e):
    for x in range(len(custBday.get())):
        monthCheck = False
        dayCheck = False
        yearCheck = False
        minorCheck = False
        slash = False

        monthCheck31 = False
        monthCheck30 = False
        monthCheck28= False

        if x == 9:
            #Slash Checking
            if custBday.get()[2] == "/" and custBday.get()[5] == "/":
                slash = True
                print("Slash Check = Pass")
            else:
                print("Slash Check = Fail")

            #Month Checking
            if (int(custBday.get()[0:2]) <= 12):
                monthCheck = True
                print("MonthCheck = Pass")
            else :
                print("MonthCheck = Failed")

            #Day of Month Checking
            if (int(custBday.get()[0:2]) == 1):
                monthCheck31 = True
                print("Month Days = 31")
            if (int(custBday.get()[0:2]) == 2):
                monthCheck28 = True
                print("Month Days = 28")
            if (int(custBday.get()[0:2]) == 3):
                monthCheck31 = True
                print("Month Days = 31")
            if (int(custBday.get()[0:2]) == 4):
                monthCheck30 = True
                print("Month Days = 30")
            if (int(custBday.get()[0:2]) == 5):
                monthCheck31 = True
                print("Month Days = 31")
            if (int(custBday.get()[0:2]) == 6):
                monthCheck30 = True
                print("Month Days = 30")
            if (int(custBday.get()[0:2]) == 7):
                monthCheck31 = True
                print("Month Days = 31")
            if (int(custBday.get()[0:2]) == 8):
                monthCheck31 = True
                print("Month Days = 31")
            if (int(custBday.get()[0:2]) == 9):
                monthCheck30 = True
                print("Month Days = 30")
            if (int(custBday.get()[0:2]) == 10):
                monthCheck31 = True
                print("Month Days = 31")
            if (int(custBday.get()[0:2]) == 11):
                monthCheck30 = True

            if (int(custBday.get()[0:2]) == 12):
                monthCheck31 = True
                print ("Month Days = 31")

            #Day Checking
            if monthCheck31 == True and int(custBday.get()[3:5]) <= 31 and int(custBday.get()[0:2]) != 0:
                dayCheck = True
                print ("Day Check = Pass")
            elif monthCheck30 == True and int(custBday.get()[3:5]) <= 30 and int(custBday.get()[0:2]) != 0:
                dayCheck = True
                print("Day Check = Pass")
            elif monthCheck28 == True and int(custBday.get()[3:5]) <= 28 and int(custBday.get()[0:2]) != 0:
                dayCheck = True
                print("Day Check = Pass")
            else :
                birthdateCheck.config(text="Incorrect Date Format")

            #Minor Checking
            if int(custBday.get()[6:8]) == 20 and int(custBday.get()[8:10]) <= 2:
                minorCheck = True
                print("Minor Check: Passed")
            else:
                birthdateCheck.config(text="Invalid Year")

            if  int(custBday.get()[6:8]) == 19 and int(custBday.get()[8:10] <= 99):
                minorCheck = True
                print("Minor Check: Passed")
            else:
                birthdateCheck.config(text="Invalid Year")

            #Year Checking
            if minorCheck == True and int(custBday.get()[6:8]) != 0:
                yearCheck = True
                print ("Year Check: Pass \n")
            elif minorCheck == False:
                birthdateCheck.config(text="You must be 18+ of age to save account")
                print("Customer is a Minor")

            if int(custBday.get()[6:8]) > 20:
                birthdateCheck.config(text="Invalid Year")




        else :
            birthdateCheck.config(text = "Wrong Date")

        if slash == True and monthCheck == True and yearCheck == True and dayCheck == True:
            birthdateCheck.config(text = "✓")



def msgbox (msg, titlebar):
    messagebox.showinfo(title = titlebar, message=msg)

def products():
    window = Tk()
    window.title("Products Form")
    window.geometry("550x400")
    window.configure(bg="orange")



window = Tk()
window.title("Sample Window")
window.geometry("550x400")
window.configure(bg="orange")

menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Products", command=products)
filemenu.add_command(label="Orders")
filemenu.add_separator()
filemenu.add_command(label="Close", command = window.quit)

window.config(menu=menubar)

labelTitle = Label(window, text="Customer Registration System", width=30, height=1, bg="yellow", anchor= "center")
labelTitle.config(font=("Courier", 10))
labelTitle.grid(column=2, row=1)

labelID = Label(window, text="Customer ID", width = 20, height = 1, bg = "yellow")
labelID.grid(column=1, row=2)

cID = StringVar()
custid = Text(window, width=15, height=1)
custid.grid(column=2, row=2)
custid.insert(1.0, "1")
custid.config(state = DISABLED)

labelNameCheck = Label(window, text="Last Name, First Name", width = 20, height = 1, bg = "yellow")
labelNameCheck.grid(column=3, row=3)
labelName = Label(window, text="Customer Name", width = 20, height = 1, bg = "yellow")
labelName.grid(column=1, row=3)

cName = StringVar()
custName = Entry(window, textvariable = cName)
custName.grid(column=2, row=3)

labelEmail = Label(window, text="Customer Email", width = 20, height = 1, bg = "yellow")
labelEmail.grid(column=1, row=4)


cEmail = StringVar()
custEmail = Entry(window, textvariable = cEmail)
custEmail.grid(column=2, row=4)
custEmail.bind("<KeyRelease>", keyup)

labelEmail = Label(window, text="[a-z]@[a-z].com", width = 20, height = 1, bg = "yellow")
labelEmail.grid(column=3, row=4)

labelBday = Label(window, text="Customer Birthdate", width = 20, height = 1, bg = "yellow")
labelBday.grid(column=1, row=5)

birthdate= StringVar()
custBday = Entry(window, textvariable = birthdate)
custBday.grid(column=2, row=5)
custBday.bind("<KeyRelease>", keyupBdate)


birthdateCheck = Label(window, text="mm/dd/yyyy", width = 20, height = 1, bg = "yellow")
birthdateCheck.grid(column=3, row=5)

savebtn = Button(text = "Save", command = save)
savebtn.grid(column=1)


deletebtn = Button(text = "Delete", command = delete)
deletebtn.grid(column=1)



window.mainloop()

Tags: andtextconfigfalsetruegetifcolumn
2条回答

线路

deletebtn = Button(text = "Delete", command = delete)

不带任何参数调用delete()。这不是语法错误,但是当您单击删除按钮时,会得到一个TypeError

避免此错误的三种方法:

1Use lambda在函数中传递参数

2。修改函数delete()

def delete(indexPosition=None):
    if indexPosition is None: # function called by the button, no arguments
        list.pop() # pop() with no arguments - delete the last element of list
    else: # called with an argument
        list.pop(indexPosition)
    
    createTable()

3。在全局中传递indexPosition

每次使用indexPosition = ...在函数中设置变量indexPosition,请在函数开头键入global indexPosition。这将设置名为indexPosition的全局变量,而不是局部变量。因此indexPosition可以在程序中的任何地方使用,您无需向delete函数添加一个函数:

def delete():
    list.pop(indexPosition)
    createTable()

您的错误可能与tkinter的CallWrapper类有关。您应该检查tkinter的文档以及代码调用此类的地方

class CallWrapper:
    """Internal class. Stores function to call when some user
    defined Tcl function is called e.g. after an event occurred."""

    def __init__(self, func, subst, widget):
        """Store FUNC, SUBST and WIDGET as members."""
        self.func = func
        self.subst = subst
        self.widget = widget

    def __call__(self, *args):
        """Apply first function SUBST to arguments, than FUNC."""
        try:
            if self.subst:
                args = self.subst(*args)
            return self.func(*args)
        except SystemExit:
            raise
        except:
            self.widget._report_exception()

相关问题 更多 >