Home > Back-end >  Place two labelframes next to each other in Tkinter
Place two labelframes next to each other in Tkinter

Time:03-10

I would like to display two labelframes next to each other like in the following picture Picture1. Do you have any idea on how to do it?

When I try the labelframe don't stay in place and just resized to text into it.

This is what I want but with txt center in the labelframes

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('600x200')
style = ttk.Style(root)
root.tk.call('source', 'azure dark.tcl')
style.theme_use('azure')

lf1 = ttk.Labelframe(root, text='Labeltxt1', width=300,height=100)
lf1.grid(row=0,column=0)
lf2 = ttk.Labelframe(root, text='Labeltxt2', width=300,height=100)
lf2.grid(row=0,column=1)

root.mainloop()

And this is what I get :

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('600x200')
style = ttk.Style(root)
root.tk.call('source', 'azure dark.tcl')
style.theme_use('azure')

lf1 = ttk.Labelframe(root, text='Labeltxt1', width=300,height=100)
lf1.grid(row=0,column=0)
lf2 = ttk.Labelframe(root, text='Labeltxt2', width=300,height=100)
lf2.grid(row=0,column=1)

lb1= ttk.Label(lf1, text='txt1')
lb1.pack()
lb2= ttk.Label(lf2, text='txt2')
lb2.pack()

root.mainloop()

The labelframes don't fill the width of the window.

Hope that I've been clear enough. Thanks for the help.

CodePudding user response:

You need to tell the layout manager that the two Labelframe fill all the available space by using root.rowconfigure() and root.columnconfigure(). Also need to specify sticky='nsew' in .grid(...). In this case, you don't need to specify the width and height options of the Labelframe.

...
root.rowconfigure(0, weight=1)
root.columnconfigure((0,1), weight=1)

lf1 = ttk.Labelframe(root, text='Labeltxt1')
lf1.grid(row=0, column=0, sticky='nsew')
lf2 = ttk.Labelframe(root, text='Labeltxt2')
lf2.grid(row=0, column=1, sticky='nsew')
...
  • Related