Search code examples
tkinterresizealignment

TKInter Resizing/Alignment


How can I get the Print & Help buttons to appear immediately next to each other aligned to at the right side of the frame at all times?

import tkinter as tk

root = tk.Tk() 

# Create the frame with a height of 40 pixels
frame = tk.Frame(root, height=40, bg="goldenrod")
frame.pack(fill="both")

# Create the buttons and add them to the frame
fbutton = tk.Button(frame, text=" |< ")     .grid(row=0, column=0, sticky="w")
pbutton = tk.Button(frame, text=" < ")      .grid(row=0, column=1, sticky="w")
nbutton = tk.Button(frame, text=" > ")      .grid(row=0, column=2, sticky="w")
lbutton = tk.Button(frame, text=" >| ")     .grid(row=0, column=3, sticky="w")
ebutton = tk.Button(frame, text=" Edit ")   .grid(row=0, column=4, sticky="w")
cbutton = tk.Button(frame, text=" Cancel ") .grid(row=0, column=5, sticky="w")
sbutton = tk.Button(frame, text=" Save ")   .grid(row=0, column=6, sticky="w")
pbutton = tk.Button(frame, text=" Print ", bg="khaki")  .grid(row=0, column=7, sticky="e")
hbutton = tk.Button(frame, text=" Help ", bg="indianred")   .grid(row=0, column=8, sticky="e")


# Configure the rows and columns to expand
frame.rowconfigure(0, weight=0, minsize=40)

frame.columnconfigure(0, weight=0)
frame.columnconfigure(1, weight=0)
frame.columnconfigure(2, weight=0)
frame.columnconfigure(3, weight=0)
frame.columnconfigure(4, weight=0)
frame.columnconfigure(5, weight=0)
frame.columnconfigure(6, weight=0)
frame.columnconfigure(7, weight=1, minsize=40)
frame.columnconfigure(8, weight=1, minsize=40)

root.mainloop()

Solution

  • If you want to put the Print and Help buttons together at the right side of the frame, remove weight=1 from frame.columnconfigure(8, ...).