I have a scrollable frame class that I borrowed from some code I found, and I'm having trouble adjusting it to fit my needs. It was managed by .pack(), but I needed to use .grid(), so I simply packed a frame (self.region) into it so I could grid my widgets inside of that. However, the widgets inside of this frame won't expand to meet the edges of the container and I'm not sure why. Theres a lot of issues similar to mine out there, but none of the solutions seemed to help. I tried using .grid_columnconfigure, .columnconfigure(), and .bind("Configure") all to no avail. Does anyone have any suggestions to get the widgets in my scrollable region to expand east and west to fill the window?
import tkinter as tk
from tkinter import ttk
class ScrollableFrame(ttk.Frame):
"""taken from https://blog.tecladocode.com/tkinter-scrollable-frames/ and modified to
allow for the use of grid inside self.region
Class that allows for the creation of a frame that is scrollable"""
def __init__(self, container, *args, **kwargs):
super().__init__(container, *args, **kwargs)
canvas = tk.Canvas(self)
scrollbar = ttk.Scrollbar(self, orient="vertical", command=canvas.yview)
self.scrollable_frame = ttk.Frame(canvas)
canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.pack(side="left", fill="both", expand=True)
canvas.rowconfigure(0, weight=1)
canvas.columnconfigure(0, weight=1)
scrollbar.pack(side="right", fill="y")
self.scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
self.scrollable_frame.rowconfigure(0, weight=1)
self.scrollable_frame.columnconfigure(0, weight=1)
self.region=ttk.Frame(self.scrollable_frame)
self.region.pack(fill='both', expand=1)
self.region.grid_rowconfigure(0, weight=1)
self.region.grid_columnconfigure(0, weight=1)
class OtherWindow():
"""data collector object and window"""
def __init__(self, window):
self.window=tk.Toplevel(window)
self.window.grid_columnconfigure(0, weight=1)
self.window.grid_columnconfigure(1, weight=1)
self.window.grid_columnconfigure(2, weight=1)
self.window.grid_columnconfigure(3, weight=1)
self.window.grid_rowconfigure(3, weight=1)
self.lbl1=ttk.Label(self.window, text="this is a sort of long label")
self.lbl1.grid(row=0, column=0, columnspan=2)
self.lbl2=ttk.Label(self.window, text="this is another label")
self.lbl2.grid(row=0, column=2)
self.lbl3=ttk.Label(self.window, text="Other information: blah blah blah blah")
self.lbl3.grid(row=0, column=3)
self.directions=ttk.Label(self.window, text='These are instructions that are kind of long and take'+\
'up about this much space if I had to guess so random text random text random text', wraplength=700)
self.directions.grid(row=1, column=0, columnspan=4)
self.scrolly=ScrollableFrame(self.window)
self.scrolly.grid(row=2, column=0, columnspan=4,sticky='nsew')
self.frame=self.scrolly.region
self.fillScrollRegion()
self.continueBtn=ttk.Button(self.window, text="Do Something", command=self.do)
self.continueBtn.grid(row=3, column=0, columnspan=4, sticky='nsew')
def fillScrollRegion(self):
"""fills scrollable region with label widgets"""
for i in range(15):
for j in range(5):
lbl=ttk.Label(self.frame, text="Sample text"+str(i)+' '+str(j))
lbl.grid(row=i, column=j, sticky='nsew')
def do(self):
pass
root=tk.Tk()
app=OtherWindow(root)
root.mainloop()
The problem is that the scrollframe container Frame is not filling the Canvas horizontally. Instead of bothering to fix some copy/paste, example scrollframe, and explain it, I'll just give you my scrollframe. It is substantially more robust than the one you are using, and the problem you are having doesn't exist with it. I've already plugged it into a version of your script below.
The solution to your scrollframe's issue is found in my on_canvas_configure method. It simply tells the container frame to be the same width as the canvas, on canvas <Configure> events.
import tkinter as tk, tkinter.ttk as ttk
from typing import Iterable
class ScrollFrame(tk.Frame):
def __init__(self, master, scrollspeed=5, r=0, c=0, rspan=1, cspan=1, grid={}, **kwargs):
tk.Frame.__init__(self, master, **{'width':400, 'height':300, **kwargs})
#__GRID
self.grid(**{'row':r, 'column':c, 'rowspan':rspan, 'columnspan':cspan, 'sticky':'nswe', **grid})
#allow user to set width and/or height
if {'width', 'height'} & {*kwargs}:
self.grid_propagate(0)
#give this widget weight on the master grid
self.master.grid_rowconfigure(r, weight=1)
self.master.grid_columnconfigure(c, weight=1)
#give self.frame weight on this grid
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
#_WIDGETS
self.canvas = tk.Canvas(self, bd=0, bg=self['bg'], highlightthickness=0, yscrollincrement=scrollspeed)
self.canvas.grid(row=0, column=0, sticky='nswe')
self.frame = tk.Frame(self.canvas, **kwargs)
self.frame_id = self.canvas.create_window((0, 0), window=self.frame, anchor="nw")
vsb = tk.Scrollbar(self, orient="vertical")
vsb.grid(row=0, column=1, sticky='ns')
vsb.configure(command=self.canvas.yview)
#attach scrollbar to canvas
self.canvas.configure(yscrollcommand=vsb.set)
#_BINDS
#canvas resize
self.canvas.bind("<Configure>", self.on_canvas_configure)
#frame resize
self.frame.bind("<Configure>", self.on_frame_configure)
#scroll wheel
self.canvas.bind_all('<MouseWheel>', self.on_mousewheel)
#makes frame width match canvas width
def on_canvas_configure(self, event):
self.canvas.itemconfig(self.frame_id, width=event.width)
#when frame dimensions change pass the area to the canvas scroll region
def on_frame_configure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
#add scrollwheel feature
def on_mousewheel(self, event):
self.canvas.yview_scroll(int(-event.delta / abs(event.delta)), 'units')
#configure self.frame row(s)
def rowcfg(self, index, **options):
index = index if isinstance(index, Iterable) else [index]
for i in index:
self.frame.grid_rowconfigure(i, **options)
#so this can be used inline
return self
#configure self.frame column(s)
def colcfg(self, index, **options):
index = index if isinstance(index, Iterable) else [index]
for i in index:
self.frame.grid_columnconfigure(i, **options)
#so this can be used inline
return self
class AuxiliaryWindow(tk.Toplevel):
def __init__(self, master, **kwargs):
tk.Toplevel.__init__(self, master, **kwargs)
self.geometry('600x300+600+200')
self.attributes('-topmost', True)
self.title('This Is Another Title') #:D
#if you reconsider things, you can accomplish more with less
labels = ["this is a sort of long label",
"this is another label",
"Other information: blah blah blah blah"]
for i, text in enumerate(labels):
ttk.Label(self, text=text).grid(row=0, column=i)
self.grid_columnconfigure(i, weight=1)
#doing it this way the text will always fit the display as long as you give it enough height to work with
instr = tk.Text(self, height=3, wrap='word', bg='gray94', font='Arial 8 bold', bd=0, relief='flat')
instr.insert('1.0', ' '.join(['instructions']*20))
instr.grid(row=1, columnspan=3, sticky='nswe')
#instantiate the scrollframe, configure the first 5 columns and return the frame. it's inline mania! :p
self.scrollframe = ScrollFrame(self, 10, 2, 0, cspan=3).colcfg(range(5), weight=1).frame
self.fillScrollRegion()
#why store a reference to this? Do you intend to change/delete it later?
ttk.Button(self, text="Do Something", command=self.do).grid(row=3, columnspan=3, sticky='ew')
def fillScrollRegion(self):
"""fills scrollable region with label widgets"""
r, c = 30, 5 #math is our friend
for i in range(r*c):
ttk.Label(self.scrollframe, text=f"row_{i%r} col_{i//r}").grid(row=i%r, column=i//r, sticky='nsew')
def do(self):
pass
class Root(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry('+550+150')
self.title('This Is A Title Probably Or Something') #:D
aux = AuxiliaryWindow(self)
self.mainloop()
Root() if __name__ == "__main__" else None
Related
I am trying to make a GUI such as this with pods, each containing their own elements such as text, images and buttons.
My goal is to make it so that the so called pods can be added to the GUI window (a scrolling capable window) at any point in the code and updated in the window shifting the previous pod to the right or down to the next row if the current row is full like the image below.
I have never messed with Tkinter before so I was wondering if anyone could help me with what steps I would need to take to make such a GUI.
Implement a class that inherits from the Frame class. You can then create as many instances of this class that you want. Since you want the pods to wrap, you can use a Text widget to hold the pods since it's the only scrollable widget that natively supports wrapping.
The "pod" class might look something like this:
class Pod(tk.Frame):
def __init__(self, parent, title, subtitle, image):
super().__init__(parent, bd=2, relief="groove")
if isinstance(image, tk.PhotoImage):
self.image = image
else:
self.image = tk.PhotoImage(file=image_path)
self.title = tk.Label(self, text=title)
self.image_label = tk.Label(self, image=self.image, bd=1, relief="solid")
self.subtitle = tk.Label(self, text=subtitle)
self.b1 = tk.Button(self, text="Button 1")
self.b2 = tk.Button(self, text="Button 2")
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure((0,1), weight=1)
self.title.grid(row=0, column=0, columnspan=2, sticky="ew")
self.image_label.grid(row=1, column=0, columnspan=2, sticky="nsew", padx=8, pady=8)
self.subtitle.grid(row=2, column=0, columnspan=2, sticky="ew")
self.b1.grid(row=3, column=0)
self.b2.grid(row=3, column=1)
You can create another class to manage these objects. If you base it on a Text widget you get the wrapping behavior for free. Though, you could also base it on a Frame or Canvas and manage the wrapping yourself.
It might look something like this:
class PodManager(tk.Text):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
self.configure(state="disabled", wrap="char")
self.pods = []
def add(self, pod):
self.pods.append(pod)
self.configure(state="normal")
self.window_create("end", window=pod)
self.configure(state="disabled")
To tie it all together, create one PodManager class, then pass one or more instances of Pod to its add method:
import tkinter as tk
...
root = tk.Tk()
pm = PodManager(root)
vsb = tk.Scrollbar(root, orient="vertical", command=pm.yview)
pm.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
pm.pack(side="left", fill="both", expand=True)
for i in range(10):
image = tk.PhotoImage(width=200,height=100)
pod = Pod(pm, f"Title #{i+1}", "More Text", image)
pm.add(pod)
root.mainloop()
I want to place my menubar frame to the top of the window like the tkinter's Menu module.
class My_Menu:
def __init__(self, master, name="Default", expand="full", mode="bar"):
##### create the frame #####
self.menus = {}
self.master = master
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=0)
self.master_frame = Frame(self.master)
self.master_frame.grid(row=0, column=0, sticky=NSEW)
self.master_frame.columnconfigure(0, weight=1)
self.master_frame.rowconfigure(0, weight=1)
self.main_frame = Frame(self.master_frame)
self.main_frame.grid(row=0, column=0, sticky=NSEW)
self.main_frame.rowconfigure(0, weight=0)
I am not sure if there is any way to do this, but a way around will be to create space to the row and use sticky to put the menu on top always.
from tkinter import *
class MenuFrame(Frame):
def __init__(self,parent,*args,**kwargs):
Frame.__init__(self,parent,*args,**kwargs)
self.b1 = Button(self,text='File',width=50)
self.b1.grid(row=0,column=0)
self.b2 = Button(self,text='Help',width=50)
self.b2.grid(row=0,column=1)
def ret_max(self):
self.update()
return self.b1.winfo_height()
root = Tk()
menu = MenuFrame(root)
menu.grid(row=0,column=0,sticky='n') # Can move this line in or out of class
height = menu.ret_max()
root.grid_rowconfigure(0,pad=height) # Make it have extra space of height of button
Button(root,text='Dummy Button').grid(row=0,column=0,sticky='s')
root.mainloop()
I'm creating a console that has a single Label packing into a frame. I'm using pack and not grid or place.
Currently the label is placed into the frame, and is configured to expand. However the expansion doesn't stretch to expand to every edge. Also, when the window is resized, the Label widget doesn't resize together with the frame.
#!/usr/bin/python3
import tkinter as tk
import time as t
class app(tk.Tk):
def __init__(self):
super().__init__()
self.canvas=tk.Canvas(self,bg="black")
self.frame=tk.Canvas(self.canvas,bg="black")
self.scrollbar=tk.Scrollbar(self,orient="vertical",command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.title("label insert and scrollbar test")
self.geometry("900x400")
self.scrollbar.pack(side=tk.RIGHT,fill=tk.Y)
self.canvas.pack(fill=tk.X,expand=1)
self.frame.pack(fill=tk.X,expand=1)
self.canvas.create_window((0,0),anchor='n',window=self.frame)
self.frame.bind("<Configure>",self.onFrameConfigure)
def onFrameConfigure(self,event):
pass
def run(self):
ntime=t.asctime(t.gmtime())
n=tk.Label(self.frame,text=ntime,anchor="nw",justify=tk.LEFT)
n.config(bg="black",fg="white",font=("Courier",12))
n.pack(fill=tk.BOTH,expand=1,padx=0)
self.after(800,self.run)
if __name__=="__main__":
m=app()
m.run()
m.mainloop()
How do i get this to work such that the Label fills the entire frame?
There are a couple of reason why your frame is not expanding to fit the canvas. You have a few issues that we need to correct first.
self.frame=tk.Canvas(self.canvas,bg="black") is not actually a frame.
onFrameConfigure is not doing anything and this is an important part to getting the frame to resize.
self.frame.bind() should be self.canvas.bind() as we are using the even that the canvas has changed in some way to trigger the function to resize the frame.
self.canvas.create_window((0,0),anchor='n',window=self.frame) needs a tag set for the frame so we can configure it in the function later.
See below code example and let me know if you have any questions.
import tkinter as tk
import time as t
class App(tk.Tk):
def __init__(self):
super().__init__()
self.canvas = tk.Canvas(self, bg="black")
self.frame = tk.Frame(self.canvas, bg="black")
self.scrollbar = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.title("label insert and scrollbar test")
self.geometry("900x400")
self.scrollbar.pack(side='right', fill='y')
self.canvas.pack(fill='x', expand=True)
self.frame.pack(fill='x', expand=True)
self.canvas.create_window((0, 0), anchor='n', window=self.frame, tags='my_frame')
self.canvas.bind("<Configure>", self.onFrameConfigure)
self.run()
def onFrameConfigure(self, event):
width = event.width
self.canvas.itemconfigure(my_frame, width=width)
self.canvas.config(scrollregion=self.canvas.bbox("all"))
def run(self):
ntime = t.asctime(t.gmtime())
n = tk.Label(self.frame, text=ntime, anchor="nw", justify='left')
n.config(bg="black", fg="white", font=("Courier", 12))
n.pack(fill='both', expand=True, padx=0)
self.after(800, self.run)
if __name__ == "__main__":
App().mainloop()
Update:
The below example changes some background colors and padding so you can see where exactly things are expanding.
import tkinter as tk
import time as t
class App(tk.Tk):
def __init__(self):
super().__init__()
self.canvas = tk.Canvas(self, bg="black")
self.frame = tk.Frame(self.canvas, bg='darkblue',)
self.scrollbar = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.title("label insert and scrollbar test")
self.geometry("900x400")
self.scrollbar.pack(side='right', fill='y')
self.canvas.pack(fill='x', expand=True)
self.frame.pack(fill='both', expand=True)
self.canvas.create_window((0, 0), anchor='n', window=self.frame, tags='my_frame')
self.canvas.bind("<Configure>", self.on_canvas_configure)
self.run()
def on_canvas_configure(self, event):
self.canvas.itemconfigure("my_frame", width=event.width, height=event.height)
self.canvas.config(scrollregion=self.canvas.bbox("all"))
def run(self):
ntime = t.asctime(t.gmtime())
tk.Label(self.frame, bg='black', fg='white', text=ntime,
font=("Courier", 12)).pack(fill='both', expand=True , padx=5, pady=5)
self.canvas.config(scrollregion=self.canvas.bbox('all'))
self.after(800, self.run)
if __name__ == "__main__":
App().mainloop()
Example results:
Just to answer my question:
'''
self.canvas.pack(fill=tk.BOTH,expand=1)
self.frame.pack(fill=tk.BOTH,expand=1,padx=1)
'''
Would expand both canvas and frame to fit the entire window. The original code only had fill=tk.X, which expands only in the X direction. For some reason i could have sworn the documentation said tk.X meant in both directions.
I use Python 2.7 and I have a scrollable frame where the canvas is not shrinking to fit the frame I want to make scrollable.
I looked at this question for an answer but it does not work when I run it:
How to resize a scrollable frame to fill the canvas?
When I print the width of the frame inside the canvas, it says 0.
I also ran the code from the answer of this question on my computer :
Scrollable Frame does not resize properly using tkinter in Python
but it will still show the white canvas to the left of the labels, and it does not resize when the labels are deleted.
It looks like this:
This is my code, based on the answer in this question:
Adding a scrollbar to a group of widgets in Tkinter
from Tkinter import *
class Scrollable_frame(Frame):
def __init__(self, parent, title, values):
self.parent = parent
Frame.__init__(self, self.parent)
self.canvas = Canvas(self, borderwidth=0, background="#ffffff")
self.scrollbar = Scrollbar(self, command=self.canvas.yview)
self.innerFrame = Radiobutton_frame(self.canvas,title,values)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.grid(row=0, column=0, sticky= N+S)
self.scrollbar.grid(row=0, column=1, sticky = N+S)
self.canvas.create_window((0,0),window = self.innerFrame,anchor="nw")
self.innerFrame.bind("<Configure>", self.set_canvas_scrollregion)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
def set_canvas_scrollregion(self, event):
width = event.width - 4
self.canvas.itemconfigure("self.innerFrame ", width=width)
self.canvas.config(scrollregion=self.canvas.bbox("all"))
class Radiobutton_frame(LabelFrame):
def __init__(self, parent, title, values):
"""
In: parent - Canvas
title - String
values - List of Int
"""
self.radiobuttons = {}
self.parent = parent
self.selection = StringVar()
self.selection.set("init")
LabelFrame.__init__(self, self.parent, text = title)
for value in values:
self.add_radiobutton(value)
def add_radiobutton(self, value):
"""
Adds a radiobutton to the frame.
In: item - String
"""
# Associate to same variable to make them function as a group
self.radiobuttons[value] = Radiobutton(master = self,
variable = self.selection,
text = value,
value = value)
self.radiobuttons[value].pack(anchor=W)
# Usage example
root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
scrollableFrame = Scrollable_frame(root, "Canvas not resizing", range(30))
scrollableFrame.grid(row=0, column=0, sticky=N+S+E+W)
if __name__ == '__main__':
root.mainloop()
I don't think above question's code snippet fits to a Minimal, Complete, and Verifiable example but at the very least it's runnable.
You have three mistakes compared to that of: How to resize a scrollable frame to fill the canvas?
The most significant of which is that in the linked question, the OP uses the option tags where you don't. Replace:
self.canvas.create_window((0,0),window = self.innerFrame,anchor="nw")
with:
self.canvas.create_window((0,0),window = self.innerFrame, anchor="nw", tags="my_tag")
Another mistake is that you're binding the event of a frame's resizing as opposed to the actual Canvas' resizing, also pointed out in Bryan's comment here. Replace:
self.innerFrame.bind("<Configure>", self.set_canvas_scrollregion)
with:
self.canvas.bind("<Configure>", self.set_canvas_scrollregion)
Lastly, tkinter doesn't seem to accept space character with tags, replace:
self.canvas.itemconfigure("self.innerFrame ", width=width)
with:
self.canvas.itemconfigure("my_tag", width=width)
Finally, you should have:
from Tkinter import *
class Scrollable_frame(Frame):
def __init__(self, parent, title, values):
self.parent = parent
Frame.__init__(self, self.parent)
self.canvas = Canvas(self, borderwidth=0, background="#ffffff")
self.scrollbar = Scrollbar(self, command=self.canvas.yview)
self.innerFrame = Radiobutton_frame(self.canvas,title,values)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.grid(row=0, column=0, sticky= N+S)
self.scrollbar.grid(row=0, column=1, sticky = N+S)
self.canvas.create_window((0,0),window = self.innerFrame,anchor="nw",
tags="my_tag")
self.canvas.bind("<Configure>", self.set_canvas_scrollregion)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
def set_canvas_scrollregion(self, event):
width = event.width - 4
self.canvas.itemconfigure("my_tag", width=width)
self.canvas.config(scrollregion=self.canvas.bbox("all"))
class Radiobutton_frame(LabelFrame):
def __init__(self, parent, title, values):
"""
In: parent - Canvas
title - String
values - List of Int
"""
self.radiobuttons = {}
self.parent = parent
self.selection = StringVar()
self.selection.set("init")
LabelFrame.__init__(self, self.parent, text = title)
for value in values:
self.add_radiobutton(value)
def add_radiobutton(self, value):
"""
Adds a radiobutton to the frame.
In: item - String
"""
# Associate to same variable to make them function as a group
self.radiobuttons[value] = Radiobutton(master = self,
variable = self.selection,
text = value,
value = value)
self.radiobuttons[value].pack(anchor=W)
# Usage example
root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
scrollableFrame = Scrollable_frame(root, "Canvas not resizing", range(30))
scrollableFrame.grid(row=0, column=0, sticky=N+S+E+W)
if __name__ == '__main__':
root.mainloop()
Using grid in tkinter, I'm trying to align a set of frames (I would love to post a picture, but I'm not allowed.)
I've two outer LabelFrames of different sizes and on top of each other which I'd like to stretch and align.
Within the bottom frame, I've a stack of several other LabelFrames and within each of the LabelFrames there is a Label. I would like for the LabelFrames to extend as much as the outer container and for each of the inner Labels to be right align with respect to the containing LabelFrame.
I've tried, without success, with various combinations of sticky, anchor, justify.
Any suggestion, recommendation?
#!/usr/bin/env python
import Tkinter as tk
class AlignTest(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.grid()
self.parent.title('Align test')
self.createMenus()
self.createWidgets()
def createMenus(self):
# Menu
self.menubar = tk.Menu(self.parent)
self.parent.config(menu=self.menubar)
# Menu->File
self.fileMenu = tk.Menu(self.menubar)
# Menu->Quit
self.fileMenu.add_command(label='Quit',
command=self.onExit)
# Create File Menu
self.menubar.add_cascade(label='File',
menu=self.fileMenu)
def createWidgets(self):
# Main frame
self.mainFrame = tk.Frame(self.parent)
self.mainFrame.grid(row=0, column=0)
# Outer LabelFrame1
self.outerLabelFrame1 = tk.LabelFrame(self.mainFrame,
text='Outer1')
self.outerLabelFrame1.grid(row=0, column=0)
# Inner Label
self.innerLabel = tk.Label(self.outerLabelFrame1,
text='This is a longer string, for example!')
self.innerLabel.grid(row=0, column=0)
# Outer LabelFrame2
self.outerLabelFrame2 = tk.LabelFrame(self.mainFrame,
text='Outer2')
self.outerLabelFrame2.grid(row=1, column=0, sticky='ew')
# Inner labelFrames each with a single labels
self.innerLabel1 = tk.LabelFrame(self.outerLabelFrame2,
bg='yellow',
text='Inner1')
self.innerLabel1.grid(row=0, column=0, sticky='ew')
self.value1 = tk.Label(self.innerLabel1,
bg='green',
text='12.8543')
self.value1.grid(row=0, column=0, sticky='')
self.innerLabel2 = tk.LabelFrame(self.outerLabelFrame2,
bg='yellow',
text='Inner2')
self.innerLabel2.grid(row=1, column=0, sticky='ew')
self.value2 = tk.Label(self.innerLabel2,
bg='green',
text='0.3452')
self.value2.grid(row=0, column=0, sticky='')
self.innerLabel3 = tk.LabelFrame(self.outerLabelFrame2,
bg='yellow',
text='Inner3')
self.innerLabel3.grid(row=2, column=0, sticky='')
self.value3 = tk.Label(self.innerLabel3,
bg='green',
text='123.4302')
self.value3.grid(row=0, column=0, sticky='')
def onExit(self):
self.parent.quit()
def main():
root = tk.Tk()
app = AlignTest(root)
app.mainloop()
if __name__ == '__main__':
main()
Without even running your code I see two problems. The first is that you aren't always using the sticky parameter when calling grid. That could be part of the problem. I've rarely ever used grid without using that parameter.
The second problem is that you aren't giving any of your rows and columns any weight. Without a positive weight, columns and rows will only ever use up exactly as much space as they need for their contents, and no more. Any extra space goes unallocated.
A good rule of thumb is that in every widget that is being used as a container for other widgets (typically, frames), you should always give at least one row and one column a positive weight.
As a final suggestion: during development it's really helpful to give each of your frames a distinctive color. This really helps to visualize how the frames are using the available space.
Thanks to Bryan's comment on weight, here is a working version of the code as potential reference. (I'll add pictures when allowed.)
#!/usr/bin/env python
import Tkinter as tk
class AlignTest(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.grid()
self.parent.title('Align test')
self.createMenus()
self.createWidgets()
def createMenus(self):
# Menu
self.menubar = tk.Menu(self.parent)
self.parent.config(menu=self.menubar)
# Menu->File
self.fileMenu = tk.Menu(self.menubar)
# Menu->Quit
self.fileMenu.add_command(label='Quit',
command=self.onExit)
# Create File Menu
self.menubar.add_cascade(label='File',
menu=self.fileMenu)
def createWidgets(self):
# Main frame
self.mainFrame = tk.Frame(self.parent)
self.mainFrame.grid(row=0, column=0)
# Outer LabelFrame1
self.outerLabelFrame1 = tk.LabelFrame(self.mainFrame,
text='Outer1')
self.outerLabelFrame1.grid(row=0, column=0)
# Inner Label
self.innerLabel = tk.Label(self.outerLabelFrame1,
text='This is a longer string, for example!')
self.innerLabel.grid(row=0, column=0)
# Outer LabelFrame2
self.outerLabelFrame2 = tk.LabelFrame(self.mainFrame,
text='Outer2')
self.outerLabelFrame2.grid(row=1, column=0, sticky='ew')
self.outerLabelFrame2.grid_columnconfigure(0, weight=1)
# Inner labelFrames each with a single labels
self.innerLabel1 = tk.LabelFrame(self.outerLabelFrame2,
bg='yellow',
text='Inner1')
self.innerLabel1.grid(row=0, column=0, sticky='ew')
self.innerLabel1.grid_columnconfigure(0, weight=1)
self.value1 = tk.Label(self.innerLabel1,
bg='green',
anchor='e',
text='12.8543')
self.value1.grid(row=0, column=0, sticky='ew')
self.innerLabel2 = tk.LabelFrame(self.outerLabelFrame2,
bg='yellow',
text='Inner2')
self.innerLabel2.grid(row=1, column=0, sticky='ew')
self.innerLabel2.grid_columnconfigure(0, weight=1)
self.value2 = tk.Label(self.innerLabel2,
bg='green',
anchor='e',
text='0.3452')
self.value2.grid(row=0, column=0, sticky='ew')
self.innerLabel3 = tk.LabelFrame(self.outerLabelFrame2,
bg='yellow',
text='Inner3')
self.innerLabel3.grid(row=2, column=0, sticky='ew')
self.innerLabel3.grid_columnconfigure(0, weight=1)
self.value3 = tk.Label(self.innerLabel3,
bg='green',
anchor='e',
text='123.4302')
self.value3.grid(row=0, column=0, sticky='ew')
def onExit(self):
self.parent.quit()
def main():
root = tk.Tk()
app = AlignTest(root)
app.mainloop()
if __name__ == '__main__':
main()