如何在tkinter中使用网格(grid)水平居中一个小部件?

huangapple go评论63阅读模式
英文:

How do I horizontally center a widget using grid in tkinter?

问题

我尝试创建一个标签,使其居中显示在屏幕上。我应该如何使用grid()来实现?这是我目前的代码。我想将“Menu”标签居中对齐。

import tkinter as tk
w = tk.Tk()
head = tk.Label(text="Menu", font=("Garamond", 40))
head.grid(column=0, row=0)
w.geometry("600x600")
app = tk.Button(text="Entrees", width=13, height=2)
main = tk.Button(text="Mains", width=13, height=2)
des = tk.Button(text="Dessert", width=13, height=2)
deal = tk.Button(text="Deals", width=13, height=2)

app.grid(column=0, row=1)
main.grid(column=1, row=1)
des.grid(column=2, row=1)
deal.grid(column=3, row=1)

w.mainloop()

我尝试过使用rowconfigure、columnconfigure和sticky属性,但无法弄清如何使其工作。

英文:

I'm trying to make a label that aligns itself to the center of the screen. How do I go about it using grid()? This is the code I currently have. I want to center the "Menu" label.

import tkinter as tk
w = tk.Tk()
head = tk.Label(text = "Menu",font = ("Garamond",40))
head.grid(column = 0,row = 0,)
w.geometry("600x600")
app = tk.Button(text="Entrees",width = 13,height = 2)
main = tk.Button(text="Mains",width = 13,height = 2)
des = tk.Button(text="Dessert",width = 13,height = 2)
deal = tk.Button(text="Deals",width = 13,height = 2)

app.grid(column = 0, row = 1)
main.grid(column = 1, row = 1)
des.grid(column = 2, row = 1)
deal.grid(column = 3, row = 1)

w.mainloop()

I messed around with rowconfigure, columnfigure and sticky but couldn't figure out how to make it work.

答案1

得分: 0

默认情况下,小部件将在为它们分配的空间中居中对齐。由于您的 UI 使用了四列,您的标签需要跨越这四列。

head.grid(column=0, row=0, columnspan=4)

标签可能仍然没有居中对齐,因为您尚未告诉 grid 如何处理小部件不需要的额外空间。不过,在这种情况下您想要发生什么不太清楚。作为一个经验法则,我建议始终至少给一个行和一列分配非零的 weight。否则,额外的空间将出现在所有小部件的右侧和下方。

例如,您可以给所有列分配相等的权重,以便均匀分配所有额外的空间。

w.grid_columnconfigure((0, 1, 2, 3), weight=1)

另一个解决方案可能是将按钮放在由 grid 管理的单独的框架中,然后使用 pack 来布局标题和包含按钮的框架。

英文:

By default, widgets will be centered in the space allocated to them. Since your UI uses four columns, your label needs to span those four columns.

head.grid(column = 0,row = 0, columnspan=4)

The label still might not be centered, since you haven't instructed grid on what to do with any extra space that is not required by the widgets. It's not clear what you want to have happen in that case, though. As a rule of thumb I recommend always giving at least one row and one column a non-zero weight. Otherwise, extra space will just appear to the right and below all of your widgets.

For example, you might want to give all columns equal weight so that they all extra spaces is evenly distributed.

w.grid_columnconfigure((0,1,2,3), weight=1)

Another solution could be to use put the buttons in a separate frame managed by grid, and then use pack to lay out the header and the frame with the buttons.

huangapple
  • 本文由 发表于 2023年6月30日 03:18:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定