Search code examples
pythontkinter

Cant change height/width of button in tkinter grid button


I have multiple buttons all being created via ttk.Button() where it asks for the Tk variable name, text, and command. I add them to the grid via varname.grid where it puts it in a row, column, padding, and for input for sticky. my problem is adding the height= or width= in both of them.

i tried adding height= and width= in both ttk.Button and varname.grid with them both returning errors. I've attached the tkinter part of my code without my commands to make it more simple for others, however I can add the functions in if it would make it easier for others. Thanks for the help

# Main GUI window
window = Tk()
window.title("Media Player")

# Create buttons
select_button = ttk.Button(window, text="Select File", command=select_file)
play_pause_button = ttk.Button(window,text="Play", command=play_music)
stop_button = ttk.Button(window, text="Stop", command=stop_music)
rewind_button = ttk.Button(window, text="Rewind", command=lambda: rewind_music())
fast_forward_button = ttk.Button(window, text="Fast Forward", command=lambda: fast_forward_music())
skip_button = ttk.Button(window, text="Skip Song", command=lambda: play_next())
previous_song_button = ttk.Button(window, text="Previous Song", command=lambda: play_previous())

# Create playlist controls
playlist_label = ttk.Label(window, text="Playlist")
playlist_box = Listbox(window, selectbackground="#3498db", selectforeground="white")
remove_button = ttk.Button(window, text="Remove", command=remove_from_playlist)
shuffle_button = ttk.Button(window, text="Shuffle", command=shuffle_playlist)
clear_button = ttk.Button(window, text="Clear", command=clear_playlist)

# Create volume control
volume_label = ttk.Label(window, text="Volume")
volume_scale = ttk.Scale(window, from_=0.0, to=1.0, orient="horizontal", command=set_volume)
volume_scale.set(volume)  # Set initial volume value

# Create status label
status_label = ttk.Label(window, text="", anchor="center")

# Position the buttons and widgets
select_button.grid(row=0, column=0, padx=10, pady=10)
play_pause_button.grid(row=0, column=1, padx=10, pady=10)
stop_button.grid(row=0, column=2, padx=10, pady=10)
rewind_button.grid(row=0, column=3, padx=10, pady=10)
fast_forward_button.grid(row=0, column=4, padx=10, pady=10)
skip_button.grid(row=0, column=5, padx=10, pady=10)
previous_song_button.grid(row=0, column=6, padx=10, pady=10)
playlist_label.grid(row=1, column=0, padx=10, pady=10, sticky="w")
playlist_box.grid(row=2, column=0, rowspan=4, columnspan=5, padx=10, pady=10, sticky="nsew")
remove_button.grid(row=2, column=5, padx=5, pady=10, sticky="w")
shuffle_button.grid(row=2, column=6, padx=5, pady=10, sticky="w")
clear_button.grid(row=5, column=5, padx=5, pady=10, sticky="w")
volume_label.grid(row=6, column=0, padx=10, pady=5, sticky="w")
volume_scale.grid(row=6, column=1, columnspan=4, padx=10, pady=5, sticky="we")
status_label.grid(row=7, column=0, columnspan=6, padx=10, pady=5, sticky="we")

# Configure grid weights
window.grid_rowconfigure(2, weight=1)
window.grid_columnconfigure(0, weight=1)

# Start the GUI event loop
window.mainloop()

Solution

  • Configuration of height is not supported in ttk.Button per Changing ttk Button Height in Python

    However width is supported by ttk.Button.

    I don't see where you are trying to add width in your example code, so I've taken two of the buttons (remove_button, and shuffle_button) and placed a width of 100 on remove_button.

    <-SNIP->

    remove_button = ttk.Button(window, text="Remove", width=100, command=remove_from_playlist)
    shuffle_button = ttk.Button(window, text="Shuffle", command=shuffle_playlist)
    

    <-SNIP->

    In the future, you can see what config keys are supported for a tk widget with:

    print(remove_button.config().keys())
    # Supported config keys:
    # dict_keys(['command', 'default', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'image', 'compound', 'padding', 'state', 'cursor', 'style', 
    'class'])
    

    Additionally, for ttk widgets you can check what ttk.Style keys are supported for a specific ttk widget with:

    print(ttk.Style().configure(remove_button.winfo_class()).keys())
    # Supported Style config keys:
    # dict_keys(['padding', 'anchor', 'width'])
    

    where remove_button is the name of the widget you are querying. In this case, I am querying the remove_button key.

    Image showing example code result