Search code examples
pythonpython-3.xtkintercustomtkinter

Add text or label to Custom TKinter Scrollable Frame


Sorry, this is my first question!!!

I want to add a list of names in a ScrolllableFrame from Custom TKinter. I am currently using CTkLabel for the names.

import customtkinter

list_names = ["Liam", "Olivia", "Noah", "Emma"]

root = customtkinter.CTk()
root.geometry("1000x550")
root.title("Test")

frame = customtkinter.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=True)

other_frame = customtkinter.CTkScrollableFrame(master=frame, height=350, width=250, label_text="List").pack(pady=40)

for name in list_names:
        customtkinter.CTkLabel(master=other_frame, text=name).pack(pady=10)

root.mainloop()

This is what happens when I run the code, instead of the names being in the ScrollableFrame


Solution

  • The problem in you code comes from the 12th line (when you define other_frame). On the same line, you created the frame and packed it. But because you did all of that in one single line, the other_frame variable contains the return of the .pack() method (in this case None).

    To make your code work you should replace line 12 by this:

    other_frame = customtkinter.CTkScrollableFrame(master=frame, height=350, width=250, label_text="List")
    other_frame.pack(pady=40)
    

    The full code is now:

    import customtkinter
    
    list_names = ["Liam", "Olivia", "Noah", "Emma"]
    
    root = customtkinter.CTk()
    root.geometry("1000x550")
    root.title("Test")
    
    frame = customtkinter.CTkFrame(master=root)
    frame.pack(pady=20, padx=60, fill="both", expand=True)
    
    other_frame = customtkinter.CTkScrollableFrame(master=frame, height=350, width=250, label_text="List")
    other_frame.pack(pady=40)
    
    for name in list_names:
            customtkinter.CTkLabel(master=other_frame, text=name).pack(pady=10)
    
    root.mainloop()
    

    Hope I helped you, have a nice day.

    PS: you never have to excuse for asking a question ;)