在窗口上填充框架调整大小

时间:2015-02-26 10:35:25

标签: python tkinter

在输入字段中输入文件名并单击“打开”按钮时,将显示内容。

我希望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 个答案:

答案 0 :(得分:1)

您应该为放置文字的列和行指定weight

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

这告诉Tkinter它应该将任何额外的空间分配给该行和列,这将使细胞在调整大小时生长。

您可能还想将sticky=ALL添加到e1,因此会根据其下方的文字调整大小。