2011-12-09 19 views
3

我有以下代碼。我的問題是我無法正確調整幀的大小。當我運行程序時,一切都如預期的那樣。但是當我調整它的大小時,我想保留原始視圖。無法調整多個網格的大小

from Tkinter import * 
import os 
import sys 


ALL=N+S+E+W 

class Application(Frame): 

    def __init__(self,master=None): 
     Frame.__init__(self,master) 
     self.master.rowconfigure(0,weight=1) 
     self.master.columnconfigure(0,weight=1) 
     self.grid(sticky=ALL) 

     self.rowconfigure(0,weight=1) 
     myframe1=Frame(self,bg='green') 
     myframe1.bind("<Button-1>",self.handler1) 
     myframe1.grid(row=0,column=0,rowspan=1,columnspan=2,sticky=ALL) 
     self.rowconfigure(1,weight=1) 
     myframe2=Frame(self,bg='blue') 
     myframe2.bind("<Button-1>",self.handler2) 
     myframe2.grid(row=1,column=0,rowspan=1,columnspan=2,sticky=ALL) 


     buttons=('Red','Blue','Green','Black') 
     button=[0]*4 
     for c in range(4): 
      self.rowconfigure(c+2,weight=1) 
      self.columnconfigure(c,weight=1) 
      button[c]=Button(self,text="{0}".format(buttons[c]),command=lambda  x=buttons[c]:self.colors(x)) 
      button[c].grid(row=2,column=c,sticky=E+W) 

     self.columnconfigure(4,weight=1) 
     self.rowconfigure(6,weight=1) 
     button1=Button(self,text='{0}'.format('Open'),command=self.content) 
     button1.grid(row=2,column=4,sticky=E+W) 


     f=Frame(self,bg='red') 
     self.myentry=Entry(f) 
     self.myentry.grid(row=0,column=4,sticky=ALL) 
     self.text=Text(f) 
     self.text.grid(row=1,column=4,sticky=ALL) 
     f.grid(row=0,column=2,rowspan=2,columnspan=3,sticky=ALL) 

     ... 

我試過很多的組合rowconfigurecolumnconfigurerowspancolumnspan,但我失敗了!

我原來的看法是:

enter image description here

在另一個方向:

enter image description here

enter image description here

在一個方向上調整大小後白色區域是我想要調整大小的Text小部件(也是藍色和綠色區域)。

回答

9

你的問題是,你似乎不太理解網格是如何工作的。例如,您只在紅框中放置了兩個小部件(self.myentry和self.text),但您將它們放在第2和第4列中。您是否知道這些列是相對於它們的父項而不是GUI作爲整個?你想讓他們在第0欄的紅框,那麼你想在第二欄的紅框它的父母。

解決這個問題的方法是分而治之。首先,將主屏幕劃分爲邏輯部分,然後佈置這些邏輯部分,以便正確調整大小。然後,對於每個零件內的任何東西,泡沫,沖洗重複。使用框架進行組織是最好的選擇。

下面是我將如何解決您的問題(雖然肯定有不止一種方法來解決這個問題)。首先,你有兩個主要的屏幕區域:頂部有綠色,藍色和紅色框架及其內容,底部則保存按鈕。頂部區域應在各個方向上生長和縮小,底部區域只在X方向上生長。我將爲此創建兩個框架,每個部分一個,並使用pack,因爲pack是最簡單的幾何管理器。頂部框架應配置爲填充兩個方向並展開。底部(帶按鈕)只能填充X方向。

您現在有兩個彼此獨立的區域,並具有適當的調整行爲:「主」區域和「工具欄」區域。您可以自由安排這些幀的內容,但無需擔心如何影響主佈局。

在底部框架中,如果您希望所有小部件的大小相同,請使用pack並使它們全部填充X並展開,並且它們將同樣填充該區域。如果您希望它們的大小不同,請使用網格,以便可以分別控制每個列。

對於頂部,它有三個子部分:紅色,綠色和藍色的框架。由於它們不是全部水平或垂直排列,我會使用網格。在單元格0,0中放置綠色,在單元格0,1中放置藍色,並在放置兩行的單元格1,1中放入紅色。給0行和1列1的權重,以便佔用所有的鬆弛。

正如我之前寫的,這不是「分而治之」這一特定問題的唯一方法。與其將主應用視爲兩部分 - 頂部和底部,頂部有三個子部分,另一種選擇是看到主窗口有四個部分:綠色,藍色,紅色和工具欄。關鍵是不要挑完美的定義,但向下打破布局問題,從外部的工作塊

這裏是一個工作示例:

from Tkinter import * 

ALL=N+S+E+W 

class Application(Frame): 

    def __init__(self,master=None): 
     Frame.__init__(self,master) 

     # the UI is made up of two major areas: a bottom row 
     # of buttons, and a top area that fills the result of 
     # UI 
     top_frame = Frame(self) 
     button_frame = Frame(self) 

     button_frame.pack(side="bottom", fill="x") 
     top_frame.pack(side="top", fill="both", expand=True) 

     # top frame is made up of three sections: two smaller 
     # regions on the left, and a larger region on the right 
     ul_frame = Frame(top_frame, background="green", width=200) 
     ll_frame = Frame(top_frame, background="blue", width=200) 
     right_frame = Frame(top_frame, background="red") 

     ul_frame.grid(row=0, column=0, sticky=ALL) 
     ll_frame.grid(row=1, column=0, sticky=ALL) 
     right_frame.grid(row=0, column=1, rowspan=2, sticky=ALL) 
     top_frame.columnconfigure(1, weight=1) 
     top_frame.rowconfigure(0, weight=1) 
     top_frame.rowconfigure(1, weight=1) 

     # the right frame is made up of two widgets, an entry 
     # on top and a text below 
     entry = Entry(right_frame) 
     text = Text(right_frame) 

     entry.pack(side="top", fill="x") 
     text.pack(side="top", fill="both", expand=True) 

     # the button frame has five equally spaced buttons 
     for color in ('Red', 'Blue', 'Green', 'Black'): 
      b = Button(button_frame, text=color) 
      b.pack(side="left", fill="x", expand=True) 
     quit_button = Button(button_frame, text="Quit") 
     quit_button.pack(side="left", fill="x", expand=True) 

root = Tk() 
app = Application(root) 
app.pack(fill="both", expand=True) 
root.mainloop() 
+0

:感謝您的directions.I沒儘管如此,你還是要說:「你只是在紅框中放置兩個小部件(self.myentry和self.text),但你把它們放在第2和第4列中。」我把它們都放在了第4列。當你說他們與他們的父母相關時,你的意思是f.grid(row = 0,column = 2)?那是什麼意思?我認爲我的問題在於for循環。原始視圖,佈局很好。調整大小是一個問題(我不希望紅色背景,文本和輸入小部件必須在其位置ition) – George

+0

我對第2列與第4列的錯誤。但是,當只有一個小部件時,將它放入第4列是沒有意義的。您的問題在循環中不是*,它與行和列的權重以及不正確使用列有關。當你說'f.grid(row = 0,...)'時,f被放在'self'(它的父)的第0行中。當你說'self.text.grid(row = 1,...)',那是f *的第一行*,因爲那是它的父親。 f在第0行沒有任何結果。每個容器('self'和'f')都有自己的「網格」可以這麼說。 –

+0

:好吧,我糾正了f幀,但仍然是相同的。按鈕是好的,所以還有什麼我必須改變?我無法確定! – George