【问题标题】:How to center a frame with Tkinter?如何使用 Tkinter 使框架居中?
【发布时间】:2020-11-23 01:46:41
【问题描述】:

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

我编写了一个代码,其中 4 个按钮位于一个框架中,但我不知道如何使框架在窗口中居中。我尝试了几种方法,例如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()

【问题讨论】:

  • 试试frame.place(relx=0.5, rely=0.5, anchor='c')

标签: python python-3.x tkinter centering


【解决方案1】:

对我有用

这里是代码

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2021-09-04
    • 1970-01-01
    相关资源
    最近更新 更多