2016-09-29 62 views
0

我在这里抓我的头。我在Tkinter很新。我试图找出一些基本的东西,比如在一个框架中放置一个标签。我遇到的问题是显示标签时,它不会继承父级的大小。实际上,它实际上会改变放置它的框架的大小。我究竟做错了什么?最后,我想在不同框架中添加更多的标签和按钮,而不在其中添加BG颜色。一帧内的标签(Tkinter)

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) 
+1

你是什么意思“[标签]不会继承父母的大小”?你期待字体改变吗?此外,tkinter小部件旨在缩小(或扩大)为完全适合其子女。 –

回答

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',在这个例子中我不完全确定这是'LabelFrame'。

希望这会有所帮助! Luke