2015-02-26 73 views
0

當您在輸入字段中輸入文件名並單擊「打開」按鈕時,將顯示內容。在窗口中填充框架調整大小

我想讓Entry和Text小部件完全填充Frame3(紅色),並在應用程序窗口調整大小時繼續這樣做。我如何實現這一目標?

這是我的代碼:

from Tkinter import * 

ALL = N+S+W+E 

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) 


     def handler(event): 
      print("clicked at", event.x, event.y) 

     def show_entry_fields(): 
      #print e1.get() 
      f=open(e1.get()) 
      out_put=f.read() 
      l1=Label(f3, text=out_put,fg="purple").grid(row=5,column=2) 
      return out_put 
     def call_red(): 
      out_put=show_entry_fields() 
      Label(f3, text=out_put,fg="red").grid(row=5,column=2) 
     def call_green(): 
      out_put=show_entry_fields() 
      Label(f3, text=out_put,fg="green").grid(row=5,column=2) 
     def call_blue(): 
      out_put=show_entry_fields() 
      Label(f3, text=out_put,fg="blue").grid(row=5,column=2) 
     def call_black(): 
      out_put=show_entry_fields() 
      Label(f3, text=out_put,fg="black").grid(row=5,column=2) 

     for r in range(4): 
      self.rowconfigure(r, weight=1) 
     self.master.rowconfigure(0, weight=1) 
     self.columnconfigure(0, weight=1) 
     b1=Button(self, text="Red",command=call_red).grid(row=5, column=0, sticky=ALL) 

     self.columnconfigure(1, weight=1) 
     b2=Button(self, text="Blue",command=call_blue).grid(row=5, column=1, sticky=ALL) 


     self.columnconfigure(2, weight=1) 
     b3=Button(self, text="Green",command=call_green).grid(row=5, column=2, sticky=ALL) 


     self.columnconfigure(3, weight=1) 
     b4=Button(self, text="Black",command=call_black).grid(row=5, column=3, sticky=ALL) 

     self.columnconfigure(4, weight=1) 
     b5=Button(self, text="Open",command=show_entry_fields).grid(row=5, column=4, sticky=ALL) 
     #------------------------------ 

     f1 = Frame(self, bg="blue") 
     f1.grid(row=0, column=0, rowspan=2,columnspan=2, sticky=ALL) 
     f1.bind("<Button-1>", handler) 
     f1.focus() 

     f2 = Frame(self, bg="green") 
     f2.grid(row=2, column=0, rowspan=2,columnspan=2, sticky=ALL) 
     f2.bind("<Button-1>", handler) 
     f2.focus() 

     f3 = Frame(self, bg="red") 
     f3.grid(row=0, column=2, rowspan=4, columnspan=4, sticky=ALL) 

     l=Label(f3, text="Enter File Path:").grid(row=1) 

     e1 = Entry(f3) 
     e1.grid(row=1,column=2) 


root = Tk() 
app = Application(master=root) 
app.mainloop() 

回答

1

應先分配weight到您放置文本列和行:

f3.grid_columnconfigure(2, weight=1) 
f3.grid_rowconfigure(5, weight=1) 

這告訴Tkinter的,它應該分配額外的空間到那個行和列,這將使細胞在調整大小時生長。

您可能還想將sticky=ALL添加到e1,因此它會調整其下方的文字。