当我要求它删除整个字符串时,仅删除字符串的第一个字符

问题描述 投票:0回答:1

我使用

sqlite
tkinter
对我的计算机科学课程作业进行了数据验证检查,如下所示:

    ## Initialise valid to True
    valid = True

    ## Reset each error message if needed
    CLI_errortext_1.set("")
    ...

    ## Get values from entry fields
    CLI_Title = cli_entries[0].get()
    CLI_Forename = cli_entries[1].get()
    CLI_Surname = cli_entries[2].get()
    ...

    if not CLI_Title.isalpha():
        CLI_errortext_1.set("Invalid title. Please use letters only.")
        valid = False

    if not CLI_House_No.isdigit():
        CLI_errortext_4.set("Invalid house number. Please use numbers only.")
        valid = False

    ## Postcode format check

    while True:
        rex = re.compile("^[A-Z]{2}[0-9]{2}[0-9]{1}[A-Z]{2}$")

        if rex.match(CLI_Postcode):
            break
        else:
            CLI_errortext_8.set("Invalid postcode. Please use valid postcode format.")
            valid = False
            break

    ## Length check

    if len(CLI_Phone_No) != 11:
        CLI_errortext_9.set("Invalid phone number. Please use 11 digits.")
        valid = False
        
    ## Email format check

    while True:
        rex = re.compile("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)")

        if rex.match(CLI_Email):
            break
        else:
            CLI_errortext_10.set("Invalid email. Please use a valid email format.")
            valid = False
            break

    ## DOB type and range check

    age = None
    
    try:
        DOB_date = datetime.strptime(CLI_DOB, "%d/%m/%y")
    except ValueError:
        ## Date of birth does not match the expected format, display an error message
        CLI_errortext_11.set("Invalid DOB format. Please use valid date format (dd/mm/yy).")
        valid = False
    else:
        ## Date of birth is in the correct format, continue with the range check
        today = datetime.today()
        age = today.year - DOB_date.year - ((today.month, today.day) < (DOB_date.month, DOB_date.day))

        ## Define the acceptable age range
        min_age = 18
        max_age = 100

        ## Perform the range check
        if age < min_age or age > max_age:
            ## Age is outside the acceptable range, display an error message
            CLI_errortext_11.set(f"Invalid age. Age must be between {min_age} and {max_age}.")
            valid = False
        else:
            ## Age is within the acceptable range, clear the error message
            CLI_errortext_11.set("")
    
    ## All entries presence check
    
    for entry in cli_entries:
        if not entry.get():
            messagebox.showerror("Error", "Please fill in all required fields.")
            return

在运行我的代码时输入正确的数据类型(在最初插入错误的数据类型以检查错误文本是否出现之后)后,我希望它能够像我编码的那样删除整个字符串。不幸的是,它只删除了第一个字符,我不知道我为此做了什么。我希望整个东西都消失,而不仅仅是第一个角色。

python python-3.x sqlite validation tkinter
1个回答
0
投票

您并没有删除该消息,您只是将

""
放入框中,程序会将
""
插入到第一个字符。这意味着第一个字符被“”替换,字符串的其余部分不变。试试这个:

CLI_errortext_1.delete(0,len(CLI_errortext_1.get()))
© www.soinside.com 2019 - 2024. All rights reserved.