【问题标题】:Set grid element next to each other将网格元素彼此相邻设置
【发布时间】:2022-01-25 17:09:34
【问题描述】:

我目前使用这种网格布局,其中同一行上有 2 个元素 (x + y),第二行有另一个标签。问题是,我想在 x 旁边显示 y。现在有一些奇怪的填充。

x 是基于函数的,这意味着; 我是否可以根据 "x" 标签宽度将 "y" 设置在 "x" 旁边?强>

当前代码:

#Frame
frame_article = Frame(frame_feed, bg=darkColor)
frame_article.grid(row=0,column=0, ipadx=100, ipady=10, padx=350, pady=100)
#x
label_x = Label(frame_article, justify=LEFT, font="Roboto 15 bold", text=x, fg=blueColor, background="white")
label_x.grid(row= 1, column=0, sticky=W)
#y
label_y = Label(frame_article, justify=LEFT, font="Roboto 10 bold", text=y, fg="white", background="orange")
label_y.grid(row=1, column=0)
#Preview Text
label_content = Label(frame_article, justify=LEFT, background= darkColor, font="Roboto 15 normal", text="This is a preview text, nothing to be seen here", fg="white", wraplength=350)
label_content.grid(row=2, column=0)

【问题讨论】:

  • 行从0开始,还有其他列可以使用,为什么要放在同一列?使用一个框架并在其上放置 x 和 y,然后将该框架放在第 0 行并在第 1 行标记内容

标签: python tkinter


【解决方案1】:

在这种特殊情况下,您应该将 UI 定义为具有两列。第 0 列包含 X,第 1 列包含 y,预览文本应跨越两列。应为第 1 列提供任何额外的未分配空间。

如果你想让y元素左对齐,你还应该给anchor值“w”。

label_y.configure(anchor="w")

frame_article.grid_columnconfigure(1, weight=1)
label_x.grid(row= 1, column=0, sticky="nsw")
label_y.grid(row=1, column=1, sticky="nsew")
label_content.grid(row=2, column=0, columnspan=2, sticky="W")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2023-04-03
    • 2021-07-05
    • 2019-02-21
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多