I know that you can easily create a Tkinter Text widget that covers 100% of the width using the Pack Geometry Manager:
from tkinter import *
root = Tk()
textWidget = Text(root)
textWidget.pack(side=TOP, fill=X)
root.geometry('600x1000')
root.mainloop()
But I was wondering, how do I accomplish this same task using the Grid Geometry Manager with grid_columnconfigure?
Using grid requires these steps:
grid method of the text widget, giving it a row and column. In this case
you can use 0,0. grid method, define whether or not you want the widget to "stick"
to the sides of the space it was given. In your case you do, so you can give the
string "nsew" (north, south, east, west). grid_rowconfigure. This will cause the row to expand vertically to fill any extra spacegrid_columnconfigure. This will cause the column to expand horizontally to fill
any extra space.Note that grid_rowconfigure and grid_columnconfigure are methods to be called on the
widget that contains the text widget, not on the text widget itself. The grid method is
called on the text widget, because you are telling the text widget where it should place
itself in its parent.
You code would look like this:
from tkinter import *
root = Tk()
textWidget = Text(root)
textWidget.grid(row=0, column=0, sticky="nsew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.geometry('600x1000')
root.mainloop()
When you have a single widget that is to fill all of the space allotted to it, I recommend pack simply because you can do everything with one line of code rather than three. pack is perfect for this type of problem. grid is more suited to creating a grid of widgets, as its name applies. That being said, either is perfectly acceptable. You can mix and match grid and pack within the same program, though you can't use them both for widgets that share a common parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With