如何使用 Tkinter 将框架居中?

How to center a frame with Tkinter?

我搜索了很多有关如何使用 Tkinter 和 Python 3.8 居中框架的信息。

我写了一个代码,其中 4 个按钮位于一个框架中,但我不知道如何使框架在 window 中居中。我尝试了几种方法,例如 grid_propagate(0)grid(sticky="")pack(expand=True)... 但是什么都没有。

我正在那里分享我最近的代码。我希望你能帮助我。

window = Tk()
frame = Frame(window)
button1 = Button(frame, text="Button 1")
button2 = Button(frame, text="Button 2")
button3 = Button(frame, text="Button 3")
button4 = Button(frame, text="Button 4")

button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
button3.grid(row=1, column=0)
button4.grid(row=1, column=1)
frame.grid_propagate(0)
frame.grid(row=0, column=0, rowspan=2, columnspan=2)

window.mainloop()

对我有用

Here is the code

from tkinter import *
window = Tk()
frame = Frame(window)
button1 = Button(frame, text="Button 1")
button2 = Button(frame, text="Button 2")
button3 = Button(frame, text="Button 3")
button4 = Button(frame, text="Button 4")

button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
button3.grid(row=1, column=0)
button4.grid(row=1, column=1)
frame.place(relx=0.5,rely=0.5,anchor="c")

window.mainloop()