如何检查用户名长度是否太长

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

我有一个函数,它将接受 3 个参数,并在按下按钮后将其显示在标签上。

不起作用:((

这是该函数的代码:

def display_username(firstname, middlename, lastname):  # this function will take 3 parameters, the first name, last name, and middle name of the user

    while i==0  :
        full_name = f"{firstname} {middlename} {lastname}"  # using a formatted string to piece the component of the full name together, defining it as full_name
        label.config( text=f"Hello, {full_name}!")  # configure the label create earlier to display the full_name and with the word 'Hello' in front of it as a greeting message
        label.place(x=300,
                    y=150)  # configure the location of the label to appear acoordingly at those x and y coordinates on the app
        i == 1
        if len(firstname_entry.get(1.0,END)) > 6:
            tkinter.messagebox.showinfo("Notification",'First name is too long!, should be less than 6 characters')
        elif len(middlename_entry.get(1.0,END)) > 10:
            tkinter.messagebox.showinfo("Notification", 'Middle name is too long!, should be less than 10 characters')
        elif len(lastname_entry.get(1.0,END)) > 7:
            tkinter.messagebox.showinfo("Notification", 'Last name is too long!, should be less than 7 characters')

这是按钮的代码:

Submit_name = Button(tab1, text='Say Hello:D',command=lambda: display_username(firstname_entry.get(), middlename_entry.get(),lastname_entry.get()))  
# create a button for the using the display username function, displaying the user's information in first->last name order by getting the information typed in the texbox. The command = lambda is used to take the value of first name, middle name and last name to pass it t the display_user function
Submit_name.place(x=400, y=200, width=150, height=30)  
# define the location for the button

我希望它检查名字、中间名和姓氏的长度,并向用户发送通知,说明他们的名字太长,并且在名称达到理想要求之前不执行任何操作。我还希望它接受参数并在函数中使用 while 循环。 它不起作用,终端说它需要 1 个位置参数,但给出了 3 个

python if-statement tkinter button while-loop
1个回答
0
投票

在可以避免的情况下,请勿将

while loops
tkinter
一起使用!另外,下次请提供一个最小的可重现示例(一个可以重现错误的小型、有效的 tkinter 应用程序)。我无法用您提供的少量代码重现您的
took 1 positional argument but 3 were given
错误。您想要的事情可以轻松完成,无需任何 while 循环。

import tkinter as tk
from tkinter import messagebox


def display_username(firstname, middlename,lastname):
    if len(firstname) > 6:
        messagebox.showinfo("Notification",'First name is too long!, should be less than 6 characters')
    if len(middlename) > 10:
        messagebox.showinfo("Notification", 'Middle name is too long!, should be less than 10 characters')
    if len(lastname) > 7:
        messagebox.showinfo("Notification", 'Last name is too long!, should be less than 7 characters')
    else:
        full_name = f"{firstname}{middlename}{lastname}"
        uname.config(text=f'Hello {full_name}!')


if __name__ == "__main__":
    root = tk.Tk()

    firstname_entry = tk.Entry(root)
    firstname_entry.pack()

    middlename_entry = tk.Entry(root)
    middlename_entry.pack()

    lastname_entry = tk.Entry(root)
    lastname_entry.pack()

    uname = tk.Label(root, text='')
    uname.pack()

    Submit_name = tk.Button(root, text='Say Hello:D',command=lambda: display_username(firstname_entry.get(), middlename_entry.get(),lastname_entry.get()))
    Submit_name.pack()
    root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.