[Python] Placing text box next to text in tkinter

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm attempting to learn tkinter, how can i edit this code so that the text box is directly next to the input statement ? I've tried putting the text box in the first column with an alignment to the middle and right but it's still too far away.

    def processTitle(window):

    # allowing the user to enter their title
    Label(window, text='Enter your title: ').grid(row=1, column=0, sticky = "W", ipadx =0 , padx=10, pady=0) # text box
    usersTitle = Entry(window) # entry field
    usersTitle.grid(row=1, column=0, columnspan= 2, sticky = "", ipadx =0, padx=0, pady=20) # placing the entry field next to the first question

    # label for displaying the error message if the user's title isn't valid
    titleValidationMessage = Label(window, text="")

    def titleValidation(event):
    title = usersTitle.get() #retieving the data from the text box next to title
    correctTitles = ["mr", "mrs", "miss", "ms", "dr", "sir", "lord"]
    if title.lower() in correctTitles:
    titleValidationMessage.grid_forget() # hide the label when there's no error message
    titleValidationMessage.config(text="") # clearing error messages from any previous input for first name
    else:
    titleValidationMessage.grid(row=1, column=2, sticky = "w", ipadx = 3,padx=10) # position of the error message
    titleValidationMessage.config(text="Please enter a legal title (Mr,Mrs,Miss,Ms,Dr,Sir,Lord)")


    # Bind the focus-out event - when the user clicks out of the text box the data will be validated
    usersTitle.bind("<FocusOut>", titleValidation)

    #returing the title to the main program
    return usersTitle


    My grid layout is configued below


    # Ensure all rows have a minimal weight to prevent vertical jumping
    for i in range(20): # Configuring 10 rows
    createSAccountWindow.grid_rowconfigure(i, weight=0)

    # Column 0: Size for the entry labels
    createSAccountWindow.grid_columnconfigure(0, weight=0, minsize=100)

    # Column 1: Size for the Entry boxes
    # The sticky="nsew" on the Entry widget will ensure it fills this fixed column width.
    createSAccountWindow.grid_columnconfigure(1, weight=0, minsize=200)

    # Column 2: This column will absorb all extra horizontal space (weight=1).
    # This keeps the other columns stable and pins the error message to the left edge of this flexible space.
    createSAccountWindow.grid_columnconfigure(2, weight=1)

    Continue reading...

Compartilhe esta Página