Python tkinter how to use grid sticky

Question:

import tkinter as tk
root = tk.Tk()
root.state('zoomed')
text1 = tk.Text(state=tk.DISABLED)

scroll = tk.Scrollbar(root, command=text1.yview)
text1.configure(yscrollcommand=scroll.set)
text1.grid(row=1, column=1, sticky='we')
scroll.grid(row=1, column=2, sticky="nse")

root.mainloop()

How do I make the Text box fill the X axis? (sticky='we' did not work for me)

Asked By: yoav

||

Answers:

import tkinter as tk
root = tk.Tk()
root.state('zoomed')

text1 = tk.Text(state=tk.DISABLED)
scroll = tk.Scrollbar(root, command=text1.yview)
text1.configure(yscrollcommand=scroll.set)

text1.grid(row=1, column=0,sticky='we')
scroll.grid(row=1, column=1, sticky="ns")

root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=0)

root.mainloop()

I’ve added the parameter weight to the columns in that grid, which basically is a relationship between the columns. Please see,
What does 'weight' do in tkinter? to get a more detailed answer.

Answered By: Thingamabobs

According to title:

What is the use of sticky in Python tkinter ?

When the widget is smaller than the cell, sticky is used to indicate which sides and corners of the cell the widget sticks to. The direction is defined by compass directions: N, E, S, W, NE, NW, SE, and SW and zero.

Extracted from this website

so sticky defines on which point the cell has to stick.

But according to your requirement you need to use weight option. weight option describes how much free space should a widget use.

see this question for more details.

Answered By: Faraaz Kurawle
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.