Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center a frame within a frame in Tkinter?

I have a frame that I fixed the size of using the grid_propagate() method. I'd like to center a frame within this frame. How do I go about this?

like image 356
rectangletangle Avatar asked Nov 21 '10 23:11

rectangletangle


People also ask

Can you put frames inside of frames Tkinter?

To create a basic frame the name of the parent window is given as the first parameter to the frame() function. Therefore, to add another frame within this frame, just the name of the first frame needs to given to the second frame as the parent window.

How do you center a frame in Tkinter?

To position the Tkinters widgets, we can use place() geometry manager, where we will specify the anchor property. It can take (NW, N, NE, W, CENTER, E, SW, S, SE) as the position of the widget.

Can you put a frame inside a frame?

Adding a frame within a frame is an easy way to add depth and interest in your image. Framing photography is a great composition technique as you can use it to guide the viewer's eye to the subject, following a certain path. If you practice enough, you will soon see frames everywhere!

How do you center something in Tkinter?

To configure and align the text at the CENTER of a Tkinter Text widget, we can use justify=CENTER property.


1 Answers

pack it to fill in all directions. Add padding as needed.

Or, use place which lets you use relative or absolute positioning. You can use a relative x/y of .5/.5 and an anchor of "c" (center).

import Tkinter as tk

root=tk.Tk()
f1 = tk.Frame(width=200, height=200, background="red")
f2 = tk.Frame(width=100, height=100, background="blue")

f1.pack(fill="both", expand=True, padx=20, pady=20)
f2.place(in_=f1, anchor="c", relx=.5, rely=.5)

root.mainloop()
like image 72
Bryan Oakley Avatar answered Sep 20 '22 13:09

Bryan Oakley