Having trouble with resizing multiple grids - python

I have the following code. My problem is that I can't manage to resize properly the frames. When I run the program, everything is as expected. But when I resize it, I want to keep the original view.
from Tkinter import *
import os
import sys
ALL=N+S+E+W
class Application(Frame):
def __init__(self,master=None):
Frame.__init__(self,master)
self.master.rowconfigure(0,weight=1)
self.master.columnconfigure(0,weight=1)
self.grid(sticky=ALL)
self.rowconfigure(0,weight=1)
myframe1=Frame(self,bg='green')
myframe1.bind("<Button-1>",self.handler1)
myframe1.grid(row=0,column=0,rowspan=1,columnspan=2,sticky=ALL)
self.rowconfigure(1,weight=1)
myframe2=Frame(self,bg='blue')
myframe2.bind("<Button-1>",self.handler2)
myframe2.grid(row=1,column=0,rowspan=1,columnspan=2,sticky=ALL)
buttons=('Red','Blue','Green','Black')
button=[0]*4
for c in range(4):
self.rowconfigure(c+2,weight=1)
self.columnconfigure(c,weight=1)
button[c]=Button(self,text="{0}".format(buttons[c]),command=lambda x=buttons[c]:self.colors(x))
button[c].grid(row=2,column=c,sticky=E+W)
self.columnconfigure(4,weight=1)
self.rowconfigure(6,weight=1)
button1=Button(self,text='{0}'.format('Open'),command=self.content)
button1.grid(row=2,column=4,sticky=E+W)
f=Frame(self,bg='red')
self.myentry=Entry(f)
self.myentry.grid(row=0,column=4,sticky=ALL)
self.text=Text(f)
self.text.grid(row=1,column=4,sticky=ALL)
f.grid(row=0,column=2,rowspan=2,columnspan=3,sticky=ALL)
...
I tried many combinations of rowconfigure, columnconfigure, rowspan, columnspan, but I failed!
My original view is:
After resizing in one direction:
In another direction:
The white area is the Text widget which I want to be resizable (also the blue and green areas).

Your problem is that you seem to not quite understand how grid works. For example, you are putting only two widgets in the red frame (self.myentry and self.text) yet you are putting them in column 2 and 4. Are you aware that the columns are relative to their parent, not the GUI as a whole? You want them in column 0 of the red frame, then you want the red frame in the second column of it's parent.
The way to solve this is to divide and conquer. First, divide the main screen up into it's logical parts, and lay out those logical parts so they resize properly. Then, for anything inside each part, lather, rinse repeat. Using frames for organization is the way to go.
Here's how I would tackle your problem (though there's certainly more than one way to solve this problem). First, you have two major areas of the screen: the top portion which has the green, blue and red frames and their contents, and the bottom part which holds the buttons. The top area should grow and shrink in all directions, the bottom area only grows in the X direction. I would create two frames for this, one for each part, and use pack since pack is the simplest geometry manager. The top frame should be configured to fill both directions and expand. The bottom part (with the buttons) should only fill in the X direction.
You now have two areas that are independent of each other and have proper resize behavior: the "main" area and the "toolbar" area. You are free to arrange the inner contents of these frames however you wish without having to worry about how that affects the main layout.
In the bottom frame, if you want all the widgets to be the same size, use pack and have them all fill X and expand, and they will equally fill the area. If you want them to be different sizes, use grid so you can control each column separately.
For the top part, it has three sub-sections: the red, green and blue frames. Since they are not all arranged horizontally or vertically I would use grid. Place green in cell 0,0, blue in cell 0,1, and red in cell 1,1 spanning two rows. Give row 0 and column 1 a weight of 1 so it takes up all the slack.
As I wrote earlier, this isn't the only way to "divide and conquer" this specific problem. Instead of seeing the main app as two parts -- top and bottom, with the top part having three sub-parts, another choice is to see that your main window has four parts: green, blue, red and toolbar. The key isn't to pick the perfect definition, but to break the layout problem down into chunks working from the outside in.
Here is a working example:
from Tkinter import *
ALL=N+S+E+W
class Application(Frame):
def __init__(self,master=None):
Frame.__init__(self,master)
# the UI is made up of two major areas: a bottom row
# of buttons, and a top area that fills the result of
# UI
top_frame = Frame(self)
button_frame = Frame(self)
button_frame.pack(side="bottom", fill="x")
top_frame.pack(side="top", fill="both", expand=True)
# top frame is made up of three sections: two smaller
# regions on the left, and a larger region on the right
ul_frame = Frame(top_frame, background="green", width=200)
ll_frame = Frame(top_frame, background="blue", width=200)
right_frame = Frame(top_frame, background="red")
ul_frame.grid(row=0, column=0, sticky=ALL)
ll_frame.grid(row=1, column=0, sticky=ALL)
right_frame.grid(row=0, column=1, rowspan=2, sticky=ALL)
top_frame.columnconfigure(1, weight=1)
top_frame.rowconfigure(0, weight=1)
top_frame.rowconfigure(1, weight=1)
# the right frame is made up of two widgets, an entry
# on top and a text below
entry = Entry(right_frame)
text = Text(right_frame)
entry.pack(side="top", fill="x")
text.pack(side="top", fill="both", expand=True)
# the button frame has five equally spaced buttons
for color in ('Red', 'Blue', 'Green', 'Black'):
b = Button(button_frame, text=color)
b.pack(side="left", fill="x", expand=True)
quit_button = Button(button_frame, text="Quit")
quit_button.pack(side="left", fill="x", expand=True)
root = Tk()
app = Application(root)
app.pack(fill="both", expand=True)
root.mainloop()

Related

Tkinter widget displacement with pack()

I want to pack two buttons (left and right) and a label (in the middle) in a frame. I want the label to fill the remaining space on the frame to both sides, but the widgets get displaced vertically with this code. What's the best way to do this? The widgets don't necessarily have to be packed on a frame but I want them to align horizontally while the text size of the label can change, but the buttons need to stay in place on the far left and right side. enter image description here
import tkinter as tk
root = tk.Tk()
root.geometry('600x800')
root.configure(background='#141414')
frm = tk.Frame(root)
frm.place(x=0, y=0, width=300, height=30)
btn1 = tk.Button(frm, text='button1')
lbl = tk.Label(frm, text='Lalalalalala')
btn2 = tk.Button(frm, text='button2')
btn1.pack(side='left')
lbl.pack(fill='x')
btn2.pack(side='right')
tk.mainloop()
You can solve this problem a couple of ways. One solution is to pack the label to one side or the other rather than the top.
btn1.pack(side='left')
lbl.pack(side='left', fill='x', expand=True)
btn2.pack(side='right')
Another is to pack the buttons first, and then pack the label. With pack the order matters.
btn1.pack(side='left')
btn2.pack(side='right')
lbl.pack(fill='x', expand=True)
For an illustrated explanation of how pack works see this answer to the question Tkinter pack method confusion

Python Tkinter Grid Manager is Ignoring Sizing of Previous Items and Wrapping Frames

I was thinking the Grid manager was pretty straightforward, but I'm stuck. I basically have three frames, starting from the main window and each frame using the previous as its master.
I'm trying to create 12 blocks inside the second frame, but when I try to create the blocks it wraps around its master frame and adds extra space at the end.
I also don't understand why I can change the hScroll width to also change the size of its master frame, example being set to 1000, but changing the width of its master frame, even increasing size does nothing.
I'm trying to learn how to use Tkinter and am looking for where I am messing up conceptually here, not so much just a code fix.
# Main frame
worksheetFrame = tk.Frame(tkRoot, width=2000, height=800, background='BLUE')
worksheetFrame.grid(row=0, column=0, padx=(100, 0), pady=(60, 0))
# Horizontal scrollbar
hScroll = tk.Frame(worksheetFrame, width=1000, height=20, background='RED')
hScroll.grid(row=1, column=0)
# Month frames
for i in range(12):
monthFrame = tk.Frame(worksheetFrame, width=200, height=400, background='YELLOW')
monthFrame.config(borderwidth = 2, relief=tk.GROOVE)
monthFrame.grid(row=0, column=i, sticky=tk.W)
# Vertical scrollbars
vScroll = tk.Frame(monthFrame, width=20, height=400, background='GREEN')
vScroll.grid(row=0, column=i)
I'm trying to create 12 blocks inside the second frame, but when I try to create the blocks it wraps around its master frame and adds extra space at the end.
Nothing is wrapping. You have one green frame in column 0, and then the other 11 frames are in columns 1-11.
I think the thing you are missing is that you're trying to put one of the narrow green frames in column 0 of worksheetFrame, which is the same column that has the horizontal scrollbar. You've forced the scrollbar to be 1000 pixels wide which causes column 0 to be 1000 pixels wide. When you put a 20 pixel wide frame inside a 1000 pixel column, there's going to be a lot of unused spaced, which is what you're seeing.
I also don't understand why I can change the hScroll width to also change the size of its master frame, example being set to 1000, but changing the width of its master frame, even increasing size does nothing.
That is again due to the fact that grid and pack by default grow or shrink a container to fit its contents. Since you add a child to worksheetFrame using grid, that frame will ignore its requested width and shrink to fit its children.
If you want a single horizontal frame that spans the width of worksheetFrame, you need to use columnspan so that it spans all of the columns:
hScroll.grid(row=1, column=0, columnspan=12)

Why is inner padding only applied on the right?

MCVE
import Tkinter as tk
import ttk
root = tk.Tk()
root.minsize(200, 100)
inner_frame = ttk.LabelFrame(root, text='inner_frame')
inner_frame.grid(row=0, column=0)
button = ttk.Button(inner_frame, text='this is a button')
button.grid(row=0, column=0)
# this does not work as expected
inner_frame.grid_configure(ipadx=20)
root.mainloop()
Output
Question
Why is the inner padding for inner_frame only applied on the right? How do I apply it on both sides?
Interestingly, and I don't know if this is a bug, your problem is solved by making the column containing the button expand past its minimum horizontal size.
The minimum horizontal size of column 0 in the inner frame is the horizontal size of its content, which is the button.
If you add a inner_frame.columnconfigure(0, weight=1), then the internal padding works as expected:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
inner_frame = ttk.LabelFrame(root_frame, text="inner_frame")
inner_frame.grid(row=0, column=0)
inner_frame.columnconfigure(0, weight=1)
button = ttk.Button(inner_frame, text="this is a button")
button.grid(row=0, column=0)
# this works expected:
inner_frame.grid_configure(ipadx=20)
root.mainloop()
Note that grid_configure changes column 0 by default, so here we're adding internal padding to the cells in column 0.
I do not know exactly why this happens. It is not mentioned in the Tcl grid docs (http://tcl.tk/man/tcl8.5/TkCmd/grid.htm#M13).
Interestingly, this link does mention some special case around size of the frame using internal padding (low down the page, in the "Internal Padding" section): https://tkdocs.com/tutorial/grid.html
The difference can be subtle. Let's say you have a frame that's 20x20, and specify normal (external) padding of 5 pixels on each side. The frame will request a 20x20 rectangle (its natural size) from the geometry manager. Normally, that's what it will be granted, so it'll get a 20x20 rectangle for the frame, surrounded by a 5-pixel border.
With internal padding, the geometry manager will effectively add the extra padding to the widget when figuring out its natural size, as if the widget has requested a 30x30 rectangle. If the frame is centered, or attached to a single side or corner (using "sticky"), you'll end up with a 20x20 frame with extra space around it. If however the frame is set to stretch (i.e. a "sticky" value of "we", "ns", or "nwes") it will fill the extra space, resulting in a 30x30 frame, with no border.
But it is confusingly written, and I can't fully understand what they mean (or even if they are correct in modern Python). If someone knows what that paragraph above means, by all means comment below and let us know!
Adding internal padding to the frame as a whole
You can add internal padding by using styles or passing it directly as an argument to the frame's constructor:
ttk.LabelFrame(root, text="inner", padding=(20, 0))
The padding value there can take either:
One value, for padding on all sides
Two values, for x and y (in that order)
Four values, for padding starting at the left and going clockwise.
In your case, ipadx isn't failing. It's working as designed, it's just that the way it works isn't very intuitive, especially when you apply it to a frame.
To better visualize what is happening, lets apply the ipadx value to the button rather than the frame. That way we can see the padding relative to the label on the button.
For example, add two buttons instead of one. Give one an ipadx of 20, and give the other an ipadx of 0.
button1 = ttk.Button(inner_frame, text='this is a button')
button2 = ttk.Button(inner_frame, text='this is a button')
button1.grid(row=0, column=0, ipadx=20)
button2.grid(row=1, column=0, ipadx=0)
Notice that the button with ipadx=20 is wider, and the extra space is inside the button rather than as a margin surrounding the button.
The same thing is happening with inner_frame: when it is added to its parent, the extra space is being added inside the frame, effectively making inner_frame wider. You can't see it because it's added to the empty space already inside the frame.
Here's the important part: if you add a widget to inner_frame, grid doesn't know anything about the ipadx values applied to inner_frame -- that ipadx option only applies to inner_frame and its parent, not its children. At the point of adding widgets inside of inner_frame, grid only knows that inner_frame is X pixels wide.
To illustrate, we can add a label to the button, similar to how your original code adds a button to the frame. (note: we'll turn geometry propagation off so that it doesn't cause the button to shrink).
button1.grid_propagate(False)
label = ttk.Label(button1, text="x")
label.grid(row=0, column=0)
You should see a window that looks something like this:
See how the "x" is at the far left edge of the button? That is because it doesn't know anything about the ipadx value applied to its parent. All it knows is that the button is X pixels widget, and that it's supposed to be on the left edge of the button.
That is what's happening with you original frame and button - the button is being added inside the frame, making use of all of the space inside the frame.
So far I have figured out that using padding=... in the LabelFrame constructor produces the correct result.
If you delete the line
inner_frame.grid_configure(ipadx=20)
and use
inner_frame = ttk.LabelFrame(root, text='inner_frame', padding=[20, 0])
or alternatively
inner_frame['padding'] = [20, 0]
the result looks like this:
I have no clue why using ipadx through grid_configure does not work as expected.

TK, understanding pack and expanding space

I have a window that is resizeable, where i want to have two listboxes with scroll bars that expand to fill all space available.
when i have only one listbox packed as fill=both, expand=1, side=left and one scrollbar packed as fill=Y, expand=0, side=right then it will expand only horizontally, even though it is set to fill both directions. when i resize the window, the listbox only fill the sides. the bottom of the window remain empty.
then i moved on to add another listbox. Now instead of packing the scroll bar on right, i packed everything to left, so they are stacked. the listboxes continue to have fill=both, expand=1. Now when i resize the window both list boxes only fill vertically! the horizontal space remains empty.
what is going on? why does it ignore the vertical space with one element packed left and another right? and why it refuses to fill horizontally when everything is stacked left?
the fact that once it fill the vertical or the horizontal space leads me to believe the parent frame is expanding fine... or should i investigate that more as well?
Without seeing your actual code it's impossible to know what you're doing wrong. Here's an example to prove that pack works as documented:
import Tkinter as tk
root = tk.Tk()
lb1 = tk.Listbox(root)
lb2 = tk.Listbox(root)
vsb1 = tk.Scrollbar(root, orient="vertical", command=lb1.yview)
vsb2 = tk.Scrollbar(root, orient="vertical", command=lb2.yview)
lb1.configure(yscrollcommand=vsb1.set)
lb2.configure(yscrollcommand=vsb2.set)
lb1.pack(side="left", fill="both", expand=True)
vsb1.pack(side="left", fill="y", expand=False)
lb2.pack(side="left", fill="both", expand=True)
vsb2.pack(side="left", fill="y", expand=False)
root.mainloop()
workaround i'm using (downvote if not the right tk way)
i created two frames, both side=LEFT, expand=1, fill=BOTH and then put each pair of listbox+scrollbar there. now everything expands/fills just fine.
previously the listboxes were in the yellow frame. The ones i just created are the the blue and green.
still not sure with the pack manager would not expand the listboxes when they had scrollbars without expansion between them...

Is it possible to easily center side-stacked frames in a frame in tkinter?

I am faced with the problem to center side-stacked frames in a parent frame. I know how to center a single frame in a frame but I did not find a simple way to do this for several of them.
I get the following window
from the code below:
import Tkinter as tk
root = tk.Tk()
root.geometry("200x200")
# main frame
f = tk.Frame(root, background='black')
f.pack(expand=True, fill="both")
# two side-by-side frames inside, they fill up their space
f1 = tk.Frame(f, background='green')
f1.pack(side=tk.LEFT, expand=True, fill="both")
f2 = tk.Frame(f, background='red')
f2.pack(side=tk.LEFT, expand=True, fill="both")
# three fixed-size frames in the left frame above; I would like them to be centered in the frame
tk.Frame(f1, width=20, height=20, background="orange").pack(side=tk.LEFT, fill=None, expand=False)
tk.Frame(f1, width=20, height=20, background="white").pack(side=tk.LEFT, fill=None, expand=False)
tk.Frame(f1, width=20, height=20, background="gray50").pack(side=tk.LEFT, fill=None, expand=False)
root.mainloop()
I would like the three square frames to be centered in the green one. I had to use tk.LEFT to position them, otherwise they would have been stacked up by default.
In my complete program, the green frame is there to exclusively contain the three square frames.
What is the most standard way to center the three square frames in the green one?
While thinking about furas's comment I realized that I did not understand the true difference between expand and fill (it is still a bit vague). It is possible to center the three frames by changing the f1.pack() line to:
f1.pack(side=tk.LEFT, expand=True, fill=None)
The f1 frame is tight around the three square (fill=None) ones buts tries to take as much space as possible in all directions (expand=True), effectively being centered. Note that the green background is not visible, the frame being tight around its content.

Categories