window 调整大小时填充框架

Fill frame on window resize

当您在条目字段中输入文件名并单击"Open"按钮时,将显示内容。

我希望条目和文本小部件完全填充 Frame3(红色)并在应用程序 window 调整大小时继续这样做。我该如何实现?

这是我的代码:

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()

您应该将 weight 分配给放置文本的列和行:

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

这告诉 Tkinter 它应该将任何额外的 space 分配给该行和列,这将使单元格在调整大小时增长。

您可能还想将 sticky=ALL 添加到您的 e1,这样它会根据下面的文本调整大小。