Ask Your Question
3

How can the sizing of a tkinter frame be prevented from adjusting to the content within it?

asked 2021-10-28 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-05-01 18:00:00 +0000

scrum gravatar image

You can use the pack_propagate() and grid_propagate() methods to prevent a Tkinter frame from adjusting its size to its content.

To prevent a frame from adjusting to the content when using the pack() method:

import tkinter as tk

root = tk.Tk()

frame = tk.Frame(root)
frame.pack(fill=tk.BOTH, expand=True)

label = tk.Label(frame, text="Hello, World!")
label.pack()

frame.pack_propagate(0)  # Prevent frame from adjusting to content

root.mainloop()

To prevent a frame from adjusting to the content when using the grid() method:

import tkinter as tk

root = tk.Tk()

frame = tk.Frame(root)
frame.grid(row=0, column=0, sticky="nsew")

label = tk.Label(frame, text="Hello, World!")
label.grid(row=0, column=0)

frame.grid_propagate(0)  # Prevent frame from adjusting to content

root.mainloop()

In both examples, the pack_propagate(0) and grid_propagate(0) methods prevent the frame from adjusting its size to its content.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-28 11:00:00 +0000

Seen: 13 times

Last updated: May 01 '22