【问题标题】:placing labels within tkinter frame using grid使用网格在 tkinter 框架内放置标签
【发布时间】:2021-03-31 14:25:14
【问题描述】:

我正在尝试将标签放置在框架内的网格中。但是,它们似乎位于错误的位置。在下面的代码中,我试图将标签放置在 row=1,column=0 和 row=2,column=0 处。但是,它们被放置在 row=0,column=0 和 row=1,column=0 处。请看下面的代码。如何让 tkinkter 将标签放置在我想要的位置?

import tkinter

root = tkinter.Tk()
root.wm_title('testing grid layout')

# creates frames 
# height and width are specified in inches but do not seem to be respected
Frame00 = tkinter.Frame(root,bg = 'white', width = '1.0i', height='5.0i')

# divide the root grid into one row and one column
root.grid_rowconfigure(0, weight=0)
root.grid_columnconfigure(0, weight=0)

# place Frame00 into the root grid
Frame00.grid(column=0,row=0)

# Frame00 has four rows and one column
Frame00.grid_rowconfigure(0,weight=0)
Frame00.grid_rowconfigure(1,weight=0)
Frame00.grid_rowconfigure(2,weight=0)
Frame00.grid_rowconfigure(3,weight=0)
Frame00.grid_columnconfigure(0,weight=0)

# I'm specifying row=1 and column=0
# but it seems to be placed in row=0 and column=0
label1 = tkinter.Label(Frame00, text='Label 1')
label1.grid(row=1,column=0)

# I'm specifying row=2 and column=0
# but it seems to be placed in row=1 and column=0 
label2 = tkinter.Label(Frame00, text='Label 2')
label2.grid(row=2,column=0)

tkinter.mainloop()

【问题讨论】:

  • 空列和行不占空间,你可以在grid_configure()里面给它minsize。比如:Frame00.grid_rowconfigure(0,weight=0,minsize=30) Frame00.grid_columnconfigure(0,weight=0,minsize=30)等等……

标签: python tkinter tkinter-layout


【解决方案1】:

第 0 行为空,因此不占用空间。你可以给它一些内容,比如一个固定高度的标签。此外,您的grid_rowconfigure()grid_columnconfigure() 电话似乎表明您可能需要研究该主题。我相信默认情况下权重为 0,因此您可以选择要扩展为额外空间的权重并给它们 weight=1 并忽略其余部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多