Home > Software design >  Python - CUSTOMTKINTER How can I make both these elements appear in the same row?
Python - CUSTOMTKINTER How can I make both these elements appear in the same row?

Time:01-29

label = customtkinter.CTkLabel(master=frame, text="Username: ")
label.pack(padx=(10, 5), pady=10, side=customtkinter.TOP, anchor=customtkinter.W)
entry1 = customtkinter.CTkEntry(master=frame)
entry1.pack(padx=(0, 10), pady=10, side=customtkinter.TOP, expand=True, fill="x")

I've tried messing around with the side and anchor, but can't seem to get it to work.

Using customtkinter in Python, how can I make these 2 elements go on the same row?

Image

CodePudding user response:

Try this way. I've used grid geometry manager instead of pack.

label = customtkinter.CTkLabel(master=frame, text="Username: ")
label.grid(row=0, column=0, padx=(10, 5), pady=10, sticky=customtkinter.W)
entry1 = customtkinter.CTkEntry(master=frame)
entry1.grid(row=0, column=1, padx=(0, 10), pady=10, sticky="ew")

CodePudding user response:

  • Switched from .pack ---> .grid
  • Used columnconfigure method to add weight to columns

[Final Result:] https://i.stack.imgur.com/g6Gzf.png

Code:

import customtkinter

customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")

root = customtkinter.CTk()
root.geometry("650x175")
root.title("Bazaar Bot - Login")
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=50)
root.resizable(False, False)

#Username
label = customtkinter.CTkLabel(root, text="Username: ")
label.grid(row=0, column=0, padx=(15, 0), pady=10, sticky=customtkinter.W)
entry1 = customtkinter.CTkEntry(root)
entry1.grid(row=0, column=1, padx=(0, 10), pady=10, sticky="ew")
#Password
label = customtkinter.CTkLabel(root, text="Password: ")
label.grid(row=1, column=0, padx=(15, 0), pady=10, sticky=customtkinter.W)
entry1 = customtkinter.CTkEntry(root)
entry1.grid(row=1, column=1, padx=(0, 10), pady=10, sticky="ew")

Thanks, @Gihan

  • Related