我在这里挠我头。我在Tkinter很新。我试图找出一些基本的东西,就像在一个框架内放置标签一样。我遇到的问题是当标签显示时,它不会继承父级的大小。实际上,它实际上改变了放置它的框架的大小。我究竟做错了什么?最后,我想在不同的框架中添加更多标签和按钮,而不使用BG颜色。
main = Tk()
main.geometry('1024x768+0+0')
fm1 = LabelFrame(main, width = 1024, height = 68, bg = 'RED')
fm1.grid(row = 0, columnspan = 2)
label = Label(fm1, text = 'test')
label.grid(row = 0, sticky = N+E+S+W)
fm2 = Frame(main, width = 1024, height = 200, bg = 'BLUE')
fm2.grid(row = 1, columnspan = 2)
fm3 = Frame(main, width = 512, height = 300, bg = 'GREEN')
fm3.grid(row = 2, column = 0)
fm4 = Frame(main, width = 512, height = 300, bg = 'BLACK')
fm4.grid(row = 2, column = 1)
fm5 = Frame(main, width = 1024, height = 200, bg = 'YELLOW')
fm5.grid(row = 3, columnspan = 2)
答案 0 :(得分:0)
根据我的经验,如果您在主窗口中放置多个框架并希望它们填充放置它们的行/列,则需要使用grid_rowconfigure
配置主窗口的行/列, grid_columnconfigure
给他们一个分量。
我已经重新格式化了上面的示例,以包含配置行以显示我如何使用它们。
main = Tk()
main.geometry('1024x768+0+0')
# Congigure the rows that are in use to have weight #
main.grid_rowconfigure(0, weight = 1)
main.grid_rowconfigure(1, weight = 1)
main.grid_rowconfigure(2, weight = 1)
main.grid_rowconfigure(3, weight = 1)
# Congigure the cols that are in use to have weight #
main.grid_columnconfigure(0, weight = 1)
main.grid_columnconfigure(1, weight = 1)
# Define Frames - use sticky to fill allocated row / column #
fm1 = Frame(main, bg = 'RED')
fm1.grid(row = 0, columnspan = 2, sticky='news')
fm2 = Frame(main, bg = 'BLUE')
fm2.grid(row = 1, columnspan = 2, sticky='news')
fm3 = Frame(main, bg = 'GREEN')
fm3.grid(row = 2, column = 0, sticky='news')
fm4 = Frame(main, bg = 'BLACK')
fm4.grid(row = 2, column = 1, sticky='news')
fm5 = Frame(main, bg = 'YELLOW')
fm5.grid(row = 3, columnspan = 2, sticky='news')
# Notice 'pack' label - fine to use here as there are no conflicting grid widgets in this frame #
lab = Label(fm1, text = 'TEST LABEL')
lab.pack()
main.mainloop()
PS - 我已经删除了你指定的高度和宽度 - 这是没有必要的,因为你告诉框架填充它被“网格化”的已配置行/列中的所有空间。
最后,您的第一帧被定义为“LabelFrame”,在此示例中我不完全确定。
希望这有帮助! 路加