Home > OS >  How do i resize this labelframe in tkinter?
How do i resize this labelframe in tkinter?

Time:08-28

So I tried to make this labelframe wider by using the basic width and width option.

Here's my given minimal code.

from tkinter import *
from tkinter import ttk

app = Tk()
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.grid()
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.grid()
ttk.Label(settings, text="Length limit (in seconds)").grid()
ttk.Spinbox(settings, from_=60, to=600, width=4).grid()
app.mainloop()

minimalized preview:
what am i trying to do

used in application:
enter image description here

i want to get this labelframe little bit bigger and make the inside centered, But i had no knowledge to do so, Any help will apreciated!

CodePudding user response:

It seems like you just want to have a main_frame in the app. For simplicity I've used .pack with the options fill and expand with the constants tkinter.BOTH to stretch the widget in both (x,y) direction and True to consume extra space. (This is one of the reasons why wildcard imports are discouraged, you can be unaware of overwriting something, use import tkinter as tk instead). Same happens with the LabelFrame, you may could delete one of the containers, but that is up to you.

In LabelFrame I have configured the grid and gave the instruction that the column 0 should get the extra space with the priority/weight 1.

In addition, I gave your Spinbox a little bit more width, changed the size of the window and separated the constructor from the geometrymethod.

To get in touch with the geometry management in tkinter, you could play around with the instructions (e.g. comment some out) and see what happens.

from tkinter import *
from tkinter import ttk

app = Tk()
app.geometry('500x500')
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.pack(fill=BOTH,expand=True)
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.pack(fill=BOTH,expand=True)
settings.columnconfigure(0,weight=1)
my_label = ttk.Label(settings, text="Length limit (in seconds)")
my_label.grid()
my_spinbox = ttk.Spinbox(settings, from_=60, to=600, width=20)
my_spinbox.grid()
app.mainloop()
  • Related