2017-09-13 41 views
1

我期望下面的代碼產生的文本區域佔用屏幕的一半,因爲列的權重相等。Tkinker網格權重不符合我的預期

爲什麼文本區佔用大約2/3的屏幕,而我怎樣才能讓文本區只佔用屏幕的一半?

from tkinter import * 

root = Tk() 
root.wm_state('zoomed') 
root.columnconfigure(0, weight=1) 
root.columnconfigure(1, weight=1) 
root.rowconfigure(0, weight=1) 

root.configure(bg='red') 

info_frame = Frame(root) 
info_frame.grid(row=0, column=1, sticky="nsew") 
info_frame.columnconfigure(0, weight=1) 
info_frame.rowconfigure(0, weight=1) 

user_frame = Frame(root, bg='blue') 
user_frame.grid(row=0, column=0, sticky="nsew") 
user_frame.columnconfigure(0, weight=1) 
user_frame.rowconfigure(0, weight=1) 
user_frame.rowconfigure(1, weight=1) 

button_frame = Frame(user_frame) 
button_frame.grid(row=0, column=0, sticky="nsew") 

entry_frame = Frame(user_frame) 
entry_frame.grid(row=1, column=0, sticky="nsew") 

info_display = Text(info_frame, state=DISABLED) 
info_display.grid(row=0, column=0, sticky="nsew") 

scrollbar = Scrollbar(info_frame) 
scrollbar.grid(row=0, column=1, sticky="nsew") 

light_label = Label(entry_frame, text='Light').grid(row=0, column=0) 
light_entry = Entry(entry_frame).grid(row=0, column=1) 
current_label = Label(entry_frame, text='Current').grid(row=1, column=0) 
current_entry = Entry(entry_frame).grid(row=1, column=1) 

button1 = Button(button_frame, text='button1').grid(row=0, column=0) 
button2 = Button(button_frame, text='button2').grid(row=0, column=1) 
button3 = Button(button_frame, text='button3').grid(row=1, column=0) 
button4 = Button(button_frame, text='button4').grid(row=1, column=1) 


root.mainloop() 

回答

3

重量講述的Tkinter如何分配額外空間,它不是一個機制來保證行或列具有相同的尺寸。假設你在列0中放置一個100像素寬的小部件,在列1中放置一個寬度爲200像素的小部件,並且你給兩列賦予相同的權重。 GUI自然會嘗試300像素寬,因爲這就是你所要求的。

如果使窗口變大(無論是通過交互式調整大小,使用geometry方法,或通過縮放窗口),Tkinter的將使用權重決定如何分配額外空間。

例如,如果您強制GUI爲500像素寬,則有200個未分配的像素。假設每列的權重相同,則每列將有100個像素,使得一列爲200像素,另一列爲300.因此,儘管它們具有相同的權重,但它們將不具有相同的大小。

如果要讓列具有相同的寬度,可以使用uniform選項使列成爲統一組的一部分。在該組內,所有列將具有相同的寬度。

例如,這將保證每個列佔用空間的一半(由於那裏只有兩次與重量的字段,並且它們是相同的大小,通過定義他們必須採取了一半的窗口)

root.columnconfigure(0, weight=1, uniform="half") 
root.columnconfigure(1, weight=1, uniform="half") 

注意:您可以使用任何字符串代替"half" - 唯一的關鍵是具有相同值的所有列將具有相同的寬度。

0

網格權重分佈額外空間。請參閱reference

重量要列或行伸展,使用此選項和 供應分配額外的空間時,讓該列或行 的相對權重值。例如,如果一個小部件瓦特包含 網格佈局,這些線將分發額外 空間的四分之三到第一列和四分之一到第二列:

w.columnconfigure(0, weight=3) 
w.columnconfigure(1, weight=1) 

默認大小的文本小部件比其他東西的默認大小大得多。如果你想要更多平等的專欄,你必須發展其他的東西並縮小文本。