Reputation: 5
import tkinter as tk
submit_btn=tk.Button(root,text='click me')
submit_btn.grid(row=4,column=0,columnspan=2,pady=10,padx=10,ipadx=100)
I am a beginner to TKINTER.
Even though I put columnspan=2 the button remains in the same size i have already put some entry and label elements in rows 0,1,2,3 and with their column=1 and 0
Please help me
Why is the button size isn't changing?
Upvotes: 0
Views: 2283
Reputation: 103
Columnspan only handles the position of the button, not the actual size of the button. To ensure the button rescales to the entirety of the columns assigned add sticky='ew'
to the parameters for .grid
. The sticky parameter forces the button to stick to the sides of it's assigned columns and rows if configured for those directions. To specify the directions it should stick to put them in the string example of where to put it:
sticky='here'
The directions it accepts are: 's' for south/down/bottom, 'w' for west/left, 'n' for north/up/top, 'e' for east/right. So if you wanted it to stick to the right you'd use sticky='we'
and if you wanted it to stick to the top and bottom you'd use sticky='sn'
and if you wanted it to stick to all sides you'd use sticky='swen'
. The sticky parameter doesn't care what order or combination of directions you give it.
Example for your code:
submit_btn.grid(row=4,column=0,columnspan=2,pady=10,padx=10,ipadx=100,sticky='ew')
Upvotes: 0
Reputation: 385970
By default, widgets will be centered in the space allocated to them. To get a widget to fill the space allocated you need to use the sticky
option.
The sticky
option tells a widget to "stick" to one or more sides of the space allocated to it. The sides are designated as the compass points north, south, east, and west, and are represented as the combination of the letters "n", "s", "e", and "w".
The following example illustrates how to cause the button to grow in both height and width to fill the space it has been allocated:
submit_btn.grid(..., sticky="nsew")
Upvotes: 1
Reputation: 21
It will expand without another button.
from tkinter import *
root = Tk()
submit_btn=Button(root,text='click me')
submit_btn.grid(row=0,column=0,columnspan=1,pady=10,padx=10,ipadx=0)
submit_btn2=Button(root,text='another click')
submit_btn2.grid(row=1,column=4,columnspan=1,pady=10,padx=10,ipadx=0)
this code will create the following window:
Upvotes: 0