pythontkinterspyder

tkinter Beginner: How do I place label to the left of the entry textbox


this is my first time programming a GUI. I'm looking to have the label to the left of the entry textbox and both aligned in the center of the GUI, but I don't know to do it can anyone help that would be much appreciated

(NOTE: This a no functional GUI example. I have not added any code for how the textboxes or buttons work. They will be added later)

import tkinter as tk

root = tk.Tk()
root.geometry("400x400")
root.configure(bg='White')
root.title("Search your class information")

# Create a label with background and foreground colors
label = tk.Label(root, text="Search your for your class using one or more of the search criteria", bg="white", fg="black")
label.pack()

label = tk.Label(root, text="Enter Text:")
label.pack()

entry = tk.Entry(root, selectbackground="lightblue", selectforeground="black")
entry.pack()

# Create a button with active background and foreground colors
button = tk.Button(root, text="Search", activebackground="blue", activeforeground="white")
button.pack()

root.mainloop()

Solution

  • Simply put the label and entry in a frame:

    import tkinter as tk
    
    root = tk.Tk()
    root.geometry("400x400")
    root.configure(bg='White')
    root.title("Search your class information")
    
    # Create a label with background and foreground colors
    label = tk.Label(root, text="Search your for your class using one or more of the search criteria", bg="white", fg="black")
    label.pack()
    
    # frame for the label and entry box
    frame = tk.Frame(root)
    frame.pack()
    
    # created as child of the above frame
    label = tk.Label(frame, text="Enter Text:")
    label.pack(side='left')
    
    # created as child of above frame
    entry = tk.Entry(frame, selectbackground="lightblue", selectforeground="black")
    entry.pack(side='left')
    
    # Create a button with active background and foreground colors
    button = tk.Button(root, text="Search", activebackground="blue", activeforeground="white")
    button.pack()
    
    root.mainloop()
    

    Output

    enter image description here