Reputation: 31
I would expect the text area that the below code produces to take up half of the screen because the weights of the columns are equal.
Why does the text area take up about 2/3 of the screen instead and how do I get the text area to only take up half the screen?
from tkinter import *
root = Tk()
root.wm_state('zoomed')
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.rowconfigure(0, weight=1)
root.configure(bg='red')
info_frame = Frame(root)
info_frame.grid(row=0, column=1, sticky="nsew")
info_frame.columnconfigure(0, weight=1)
info_frame.rowconfigure(0, weight=1)
user_frame = Frame(root, bg='blue')
user_frame.grid(row=0, column=0, sticky="nsew")
user_frame.columnconfigure(0, weight=1)
user_frame.rowconfigure(0, weight=1)
user_frame.rowconfigure(1, weight=1)
button_frame = Frame(user_frame)
button_frame.grid(row=0, column=0, sticky="nsew")
entry_frame = Frame(user_frame)
entry_frame.grid(row=1, column=0, sticky="nsew")
info_display = Text(info_frame, state=DISABLED)
info_display.grid(row=0, column=0, sticky="nsew")
scrollbar = Scrollbar(info_frame)
scrollbar.grid(row=0, column=1, sticky="nsew")
light_label = Label(entry_frame, text='Light').grid(row=0, column=0)
light_entry = Entry(entry_frame).grid(row=0, column=1)
current_label = Label(entry_frame, text='Current').grid(row=1, column=0)
current_entry = Entry(entry_frame).grid(row=1, column=1)
button1 = Button(button_frame, text='button1').grid(row=0, column=0)
button2 = Button(button_frame, text='button2').grid(row=0, column=1)
button3 = Button(button_frame, text='button3').grid(row=1, column=0)
button4 = Button(button_frame, text='button4').grid(row=1, column=1)
root.mainloop()
Upvotes: 0
Views: 137
Reputation: 19144
Grid weights distribute extra space. See reference.
weight To make a column or row stretchable, use this option and supply a value that gives the relative weight of this column or row when distributing the extra space. For example, if a widget w contains a grid layout, these lines will distribute three-fourths of the extra space to the first column and one-fourth to the second column:
w.columnconfigure(0, weight=3) w.columnconfigure(1, weight=1)
The default size of the text widget is much bigger than the default size of the other stuff. If you want more equal columns, you must grow the other stuff and shrink the text.
Upvotes: 0
Reputation: 385800
The weight tells tkinter how to allocate extra space, it's not a mechanism to guarantee that columns or rows have the same size.
Let's say you place a 100 pixel wide widget in column 0, and a 200 pixel wide widget in column 1, and you give both columns equal weight. The GUI will naturally try to be 300 pixels wide, because that's what you requested.
If you make the window larger (either through interactive resizing, by using the geometry
method, or by zooming the window), tkinter will use the weight to decide how to allocate extra space.
For example, if you force the GUI to be 500 pixels wide, there are 200 unallocated pixels. Given that each column has the same weight, 100 pixels will go to each column, making one column 200 pixels and the other column 300. Thus, even though they have the same weight, they won't have the same size.
If you want columns to have an identical width, you can use the uniform
option to make the columns part of a uniform group. Within that group, all columns will have the same width.
For example, this will guarantee that each column takes up half the space (by virtue of there being only two columns with weight, and they are the same size, by definition they must take up half the window)
root.columnconfigure(0, weight=1, uniform="half")
root.columnconfigure(1, weight=1, uniform="half")
Note: you can use any string you want in place of "half"
-- the only critiera is that all columns with the same value will have the same width.
Upvotes: 3