如何让这些小部件填满母框架?

时间:2015-06-21 02:04:50

标签: python tkinter

我试图让这些条目和文本小部件占据其框架中的所有空间,但似乎它们的大小是固定的。网格工具没有帮助。

                l = Frame(self, bg=ard)
                l.grid(row=0, column = 2, rowspan = 2, columnspan = 3, sticky = ALL)
                self.enter = Entry(l, width=100)
                self.enter.grid(row=0, column=2, rowspan=1, columnspan=3, sticky=ALL)
                self.outro = Text(l, width=100, height=100)
                self.outro.grid(row=1, column=2, rowspan=1, columnspan=3, sticky=ALL)

以下是整个程序:

from tkinter import *
import os.path
root = Tk()


ALL = N+S+W+E

buttons = ['Red', 'Blue', 'Green', 'Black', 'Open']
def colorgen():
    while True:
        yield "red"
        yield "blue"
        yield "green"
        yield "orange"
        yield "purple"

color = colorgen()

def coordinates(event):
    print("X:{0} Y:{1}".format(event.x, event.y))



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

    def create_widgets(self):    

        for r in range(2):
            self.rowconfigure(r, weight=1)

            ccount = 0
            for c in range(5):
                self.columnconfigure(c, weight=1)
                ard=next(color)
                if c==0:
                    button0 = Button(self, text=buttons[c], command = lambda: self.changeoutro(0)).grid(row=4, column=c, sticky=ALL)
                elif c==1:
                    button1 = Button(self, text=buttons[c], command = lambda: self.changeoutro(1)).grid(row=4, column=c, sticky=ALL)
                elif c==2:
                    button2 = Button(self, text=buttons[c], command = lambda: self.changeoutro(2)).grid(row=4, column=c, sticky=ALL)
                elif c==3:
                    button3 = Button(self, text=buttons[c], command = lambda: self.changeoutro(3)).grid(row=4, column=c, sticky=ALL)           
                else:
                    button = Button(self, text=buttons[c], command = self.opener).grid(row=4, column=c, sticky=ALL)
                if c == 0 or c == 1:
                    l = Frame(self, bg=ard)

                    l.bind("<Button-1>", coordinates)

                    l.grid(row=c,column=0, rowspan=1, columnspan=2, sticky=ALL)

                elif c == 2:
                    l = Frame(self, bg=ard)
                    l.grid(row=0, column = 2, rowspan = 2, columnspan = 3, sticky = ALL)
                    self.enter = Entry(l, width=100)
                    self.enter.grid(row=0, column=2, rowspan=1, columnspan=3, sticky=ALL)
                    self.outro = Text(l, width=100, height=100)
                    self.outro.grid(row=1, column=2, rowspan=1, columnspan=3, sticky=ALL)



    def opener(self):
        message = []
        fname = self.enter.get()
        if os.path.isfile(fname):
            f = open(fname, 'r')
            for line in f:
                message.append(line)
            self.outro.insert(1.0, ' '.join(message))

    def changeoutro(self, c):
        self.outro.config(fg=buttons[c]) 
app = Application(master=root)             
app.mainloop()

1 个答案:

答案 0 :(得分:0)

我不确切地知道你要完成什么,所以我无法给出明确的解决方案。但是,我发现了一个常见的错误,或者导致了你所描述的问题。

首先,您正在创建一个框架l,并且您正在该框架内使用grid来管理窗口小部件。但是,您不会在该框架内为任何行或列提供权重,因此窗口小部件不会在该框架中使用任何额外的空间。您需要致电rowconfigurecolumnconfigure,以便为至少一行和一列提供正权重。

请注意,如果此框架只包含一个条目和一个文本,则对于此特定框架,使用pack而不是grid会更容易一些。您可以将条目打包到顶部,将文本打包到底部,您不必担心列权重:

self.enter.pack(side="top", fill="x", expand=False)
self.outro.pack(side="bottom", fill="both", expand=True)

其次,您可能会误解tkinter的工作原理。您告诉条目小部件跨越两行和三列,但该父小部件(l)只有一列,只有两行。使用网格行和列时,只应用 直接父窗口。如果您的GUI作为一个整体有多个行和/或列并不重要,当您在l中放置内容时,行和列仅相对于l中的行和列。

换句话说,没有理由在columnspan内使用l,因为只有一列,并且出于同样的原因,将条目和文本小部件放在第2列中是没有意义的。< / p>

相关问题