Following is my sample code containing three labels and three entry fields:
import tkinter as tk
from tkinter import *
root = Tk()
root.resizable(width=False, height=False)
root.geometry("1200x700")
root.iconbitmap(r'.\autocrest.ico')
root.title('Autocrest Job Card')
root.columnconfigure(0, weight=20)
topRowFrame= Frame(root,relief="ridge", width=1000)
topRowFrame.config(bd=1, relief=tk.SUNKEN)
topRowFrame.columnconfigure(0, weight=1)
topRowFrame.columnconfigure(1, weight=4)
topRowFrame.columnconfigure(2, weight=1)
topRowFrame.columnconfigure(3, weight=4)
topRowFrame.columnconfigure(4, weight=1)
topRowFrame.columnconfigure(5, weight=4)
topRowFrame.grid(column=0,row=0, padx=5, pady=5, sticky=W)
bookingIdLabel=tk.Label(topRowFrame, text="Booking ID")
bookingIdLabel.grid(column=0,columnspan=2, row=0, padx=5, pady=5)
bookingIdEntry=Entry(topRowFrame)
bookingIdEntry.grid(column=2,columnspan=2, row=0, padx=5, pady=5)
noLabel=tk.Label(topRowFrame, text="No")
noLabel.grid(column=4,columnspan=2, row=0, padx=5, pady=5)
noEntry=Entry(topRowFrame)
noEntry.grid(column=6,columnspan=2, row=0, padx=5, pady=5)
dateLabel=tk.Label(topRowFrame, text="Date")
dateLabel.grid(column=8,columnspan=2, row=0, padx=5, pady=5)
dateEntry=Entry(topRowFrame)
dateEntry.grid(column=10,columnspan=2, row=0, padx=5, pady=5)
root.mainloop()
All of widgets in following code occupy space equal to their width only. I want to increase space between them. Columnspan and weight has no impact.
CodePudding user response:
Columnspan and weight has no impact.
That is because you told grid
to make topRowFrame
only sticky to the left side of the window. Therefore it won't stretch to fill the whole column. Your use of root.columnconfigure(0, weight=20)
makes the column fill the window, but the widget doesn't fill the column.
Try the value "ew" (east, west) for the sticky
attribute if you want topRowFrame
to be as wide as the window:
topRowFrame.grid(column=0,row=0, padx=5, pady=5, sticky="ew")
CodePudding user response:
To be able to make widgets resizable as the screen resizes you need to use Grid.rowconfigure(root, index=0, weight=1)
. Root is the name of window/frame, index is number of the row and weight should be set to 1. You need to copy and paste this line of code and change index for the next row or use a for loop
like this:
number_of_rows = 5
for i in range(number_of_rows):
Grid.rowconfigure(root, index=i, weight=1)
and apply the same for columns with Grid.columnconfigure()
.
Once you have done that put sticky="NSEW"
so it sticks to every side of the window and fills it.