文本框不能与包含框架扩展 - TKinter

时间:2014-11-12 18:12:09

标签: python tkinter

我正在构建一个为内部数据文件提供视口的应用程序。文件有点复杂,所以我创建了三个子窗口来处理文件的不同方面。左上角提供了不同部分的轮廓,右上角提供了一系列包含数据文件中发现的错误的文本小部件,下方提供了数据文件本身的视图。为了方便所有这些,我写了一个小类,作为每个部分的框架,可以填充标签,文本框等。(下面的代码。)

我遇到的问题是右上角和下半部分的文本窗口小部件不会随其包含框架一起展开。基于对effbot.org,Stackoverflow等的各种搜索,我认为我的设置是正确的,但显然有些错误。如果我放大主窗口,每个部分都应该适应,但文本小部件不会从左向右扩展以填充新的子窗口尺寸。

非常感谢任何提示。

这是为子窗口提供功能的类:

     import Tkinter as tk

     class ScrollingChildFrame(tk.Frame):
        '''
        A Tkinter class creating a scrollable window that can be used
        in a docked multiple document interface form.  The window created here 
        allows addition of widgets to create scrolling tables (spreadsheet like),
        groups of text fields, etc.
        '''

        def __init__(self, root):

           self.count = 0

           tk.Frame.__init__(self)

           self.root = root
           self.canvas = tk.Canvas(self, height=self.winfo_height(), width=self.winfo_width() )
           self.canvas.grid(row=0, column=0, sticky='nsew')

           self.vsb = tk.Scrollbar(self, orient='vertical', command=self.canvas.yview)
           self.vsb.grid(row=0,column=1,sticky='ns')
           self.hsb = tk.Scrollbar(self, orient='horizontal', command=self.canvas.xview)
           self.hsb.grid(row=1,column=0,sticky='ew')

           self.intframe = tk.Frame(self.canvas)
           self.intframe.config(height=self.winfo_height(), width=self.winfo_width())

           self.canvas.configure(yscrollcommand=self.vsb.set, xscrollcommand=self.hsb.set)
           self.canvas.create_window(0, 0, window=self.intframe, anchor='nw')

           #set event bindings
           self.bind('<Configure>', self.OnFrameConfigure)
           self.intframe.bind('<Configure>', self.OnIntFrameConfigure)

        def OnFrameConfigure(self, event=None):
           '''
           adjust canvas when main frame adjusts
           '''
           self.canvas.configure(width=event.width - self.vsb.winfo_width()-2,
                                height=event.height - self.hsb.winfo_height()-2)

        def OnIntFrameConfigure(self, event=None):
           '''
           adjust the scrolling window when the internal frame is adjusted
           '''
           self.canvas.configure(scrollregion=self.canvas.bbox(tk.ALL))

以下是我如何使用不扩展的文本框的示例:

     import Tkinter as tk
     from scrollingchildframe import *

     class Vis_GUI:
        '''
        The main GUI class 
        '''

        def __init__(self):

           #tkinter stuff
           self.root = tk.Tk()
           self.root.geometry('500x500')

           self.create_frames()

           self.root.mainloop()


        def create_frames(self):
           '''
           Build the GUI frames
           '''      

           self.root.columnconfigure(0,weight=1)
           self.root.columnconfigure(1,weight=3)
           self.root.rowconfigure(0,weight=1)
           self.root.rowconfigure(1,weight=3)

           #data blocks
           self.block_frame = ScrollingChildFrame(self.root)
           self.block_frame.config(height=200, width=200)

           ##error list
           self.error_frame = ScrollingChildFrame(self.root)
           self.error_frame.config(height=200, width=300)

           ##data
           self.data_frame = ScrollingChildFrame(self.root)
           self.data_frame.config(height=300, width=500)

           ##populate with empty cells
           self.PopulateEmpty()

           ##place them on the grid
           self.block_frame.grid(row=0, column=0, padx=2, pady=2, sticky='nsew')
           self.error_frame.grid(row=0,column=1, padx=2, pady=2, sticky='nsew')
           self.data_frame.grid(row=1, column=0, columnspan=2, padx=2,pady=2, sticky='nsew')


        def PopulateEmpty(self):
           '''
           Populate the frames with empty contents so it doesn't look quite so empty.
           '''

           z = tk.Text(self.data_frame.intframe)
           z.insert(tk.INSERT, 'blah\nblah\nblah')
           height = float(z.index(tk.END))
           z.config( height=height, state=tk.DISABLED, wrap=tk.NONE)
           z.pack(anchor='nw', expand=1, fill=tk.X)

           z = tk.Text(self.error_frame.intframe, height=1)
           z.pack(anchor='w', expand = 1, fill=tk.X)

           z = tk.Label(self.block_frame.intframe, text = 'No file open')
           z.pack(anchor='w')

     if (__name__=="__main__"):
        wv = Vis_GUI()

1 个答案:

答案 0 :(得分:2)

框架还必须设置扩展和填充选项(您必须检查ScrollingChildFrame的作用 - 这不是对不完整代码的抱怨,只是指出下一步)。在下面的代码中仅使用pack()作为Frame将不允许它扩展。如果你想看到差异,你可以取消注释并评论另一个包。

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

top=tk.Tk()
## use separate frame instead of top
fr=tk.Frame(top)
##fr.pack()                ## does not expand
fr.pack(anchor='nw', expand=1, fill=tk.X)
z = tk.Text(fr)
insert_text="%s" % ("blah"*25) + 'blah\nblah\nblah'
z.insert(tk.INSERT, insert_text)
height = float(z.index(tk.END))
z.config( height=height, state=tk.DISABLED, wrap=tk.NONE)
z.pack(anchor='nw', expand=1, fill=tk.X)

top.mainloop()