Skip to main content
 首页 » 编程设计

python之文本框不随包含框架扩展之TKinter

2024年10月25日11youxin

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

我遇到的问题是右上部分和下部分中的文本小部件不会随着它们的包含框架展开。基于对 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() 

请您参考如下方法:

Frame 还必须设置扩展和填充选项(您将必须检查 ScrollingChildFrame 做了什么——这不是对不完整代码的提示,只是指出下一步)。在以下代码中仅对 Frame 使用 pack() 将不允许它扩展。如果您想查看不同之处,可以取消注释并注释其他包。

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