2016-09-29 61 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