【发布时间】:2021-07-26 12:54:30
【问题描述】:
我希望我的小部件填充 tkinter 中单元格提供的整个空间。 我目前正在尝试的是:
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=3)
root.columnconfigure(2, weight=3)
root.columnconfigure(3, weight=3)
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)
root.rowconfigure(2, weight=3)
font_1 = ("Helvetica", 22)
chessboard = Canvas(root, background="#ffdead")
player_1_field = Label(root, text="Player 1", anchor=W, background="grey", font=font_1)
player_2_field = Label(root, text="Player 2", anchor=W, background="grey", font=font_1)
player_1_pieces_captured = Canvas(root, background="#ff0000", height=100)
player_2_pieces_captured = Canvas(root, background="#ff0000", height=100)
timer_field = Label(root, text="Timer", anchor=W, background="grey", font=font_1)
move_history_label = Label(root, text="History", anchor=W, background="grey", font=font_1)
move_history_field = Listbox(root, background="grey", font=font_1)
chessboard.grid(row=2, column=1, columnspan=3)
player_1_field.grid(row=0, column=1, sticky=W)
player_2_field.grid(row=0, column=3, sticky=W)
player_1_pieces_captured.grid(row=1, column=1, sticky=W)
player_2_pieces_captured.grid(row=1, column=3, sticky=W)
timer_field.grid(row=0, column=2, rowspan=2)
move_history_label.grid(row=0, column=0, sticky=W)
move_history_field.grid(row=1, column=0, rowspan=2, sticky=W)
结果类似于https://imgur.com/a/jxZ6U53 我想要的结果类似于https://imgur.com/a/NxkCPNW(粗略草图)
我面临的问题是我不知道如何获取给定列/行的大小,然后按该数量获取 padx/pady。任何帮助,即使它只是资源链接,将不胜感激!
【问题讨论】:
-
如果你将
sticky = "nesw"添加到每个.grid(中,那应该是你想要的。 -
那应该是
NSEW给定的代码示例。 -
@Derek:使用字符串而不是常量将始终有效。
-
绝对正确@Bryan Oakley,只是@Henry 的“nesw”有语法错误,而@Kerialstraz 使用的是
tkinter和通配符。 -
@Derek:我不知道你所说的语法错误是什么意思。 “新”是完全有效的。使用字符串常量时,字母不必按特定顺序排列。