Search code examples
pythontkinterframecenter

Tkinter Frame Won't Center Properly (Python)


I have small_frame (filled in black) inside big_frame. I have used the place() method to center small_frame by setting relx to 0.5, but it's clearly still skewed right.

Placing by set coordinates does not work either as it is either skewed left or right.

enter image description here

from tkinter import *

root = Tk()
root.geometry('950x587')

big_frame = LabelFrame(root, width=210, height=291)
big_frame.grid(row=0, column=0, padx=10, pady=10)

small_frame = Frame(big_frame, width=198, height=123, bg='black')
small_frame.place(relx=0.5, y=64, anchor=CENTER)  # x=103 is it's coordinate equivalent

mainloop()

Solution

  • Measured it to the pixel. The small-frame is centered between the absolute boundaries of the big-frame. It is being placed perfectly.

    You might be thrown off a bit because the big-frame itself is not symmetric; there is a 1-pixel thick white line on the big-frame that is on the inside of the darker gray line on the left side, but outside of it on the right side. This white line is part of the dimensions of the big-frame. You could say that the big-frame itself is causing the "skew" through an optical illusion.

    Frankly, it's a difference of 1 pixel. If it's a serious issue, change the border of the big-frame in some way, or increase the width of the big-frame by 1 pixel.