【问题标题】:Making a Label in Tkinter fill the entire cell在 Tkinter 中制作标签填充整个单元格
【发布时间】: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:我不知道你所说的语法错误是什么意思。 “新”是完全有效的。使用字符串常量时,字母不必按特定顺序排列。

标签: python tkinter


【解决方案1】:

我包含了两个在使用grid manager 时非常有用的函数。

flexx 将使grid 对象可调整大小

grid 一个方便的函数,有助于从代码中删除数据。

这是您使用 flexxgrid 的代码

from tkinter import *

def flexx( o, r = 0, c = 0, rw = 1, cw = 1 ):
    '''flexx( o(TkContainer), r=0, c=0, rw=1, cw=1 )'''
    if r != None:
        o.rowconfigure( r, weight = rw )
    if c != None:
        o.columnconfigure( c, weight = cw )

def grid( r = 0, c = 0, s = 'nsew', rs = 1, cs = 1 ):
    '''grid( r=0, c=0, s=nsew, rs=1, cs=1 )'''
    return dict(
        row = r, column = c, rowspan = rs, columnspan = cs, sticky = s )

root= Tk()

flexx( root )
flexx( root, r = 1, c = 1, cw = 3 )
flexx( root, r = 2, c = 2, rw = 3, cw = 3 )
flexx( root, c = 3, cw = 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(grid(r = 2, c = 1, cs = 3 ))
player_1_field.grid(grid( c = 1 ))
player_2_field.grid(grid( c = 3 ))
player_1_pieces_captured.grid(grid( r = 1, c = 1 ))
player_2_pieces_captured.grid(grid( r = 1, c = 3 ))
timer_field.grid(grid( c = 2, rs = 2 ))
move_history_label.grid(grid( ))
move_history_field.grid(grid( r = 1, rs = 2 ))

mainloop()

【讨论】:

    【解决方案2】:

    如果您希望小部件填充分配给它的空间,请将sticky 参数设置为"nsew"(或常量NSEW),以便它“粘”在单元格的每一侧。

    chessboard.grid(row=2, column=1, columnspan=3, sticky=NSEW)
    player_1_field.grid(row=0, column=1, sticky=NSEW)
    player_2_field.grid(row=0, column=3, sticky=NSEW)
    player_1_pieces_captured.grid(row=1, column=1, sticky=NSEW)
    player_2_pieces_captured.grid(row=1, column=3, sticky=NSEW)
    timer_field.grid(row=0, column=2, rowspan=2, sticky=NSEW)
    move_history_label.grid(row=0, column=0, sticky=NSEW)
    move_history_field.grid(row=1, column=0, rowspan=2, sticky=NSEW)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 2011-03-14
      • 2017-09-05
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多