Python 文本框不随包含的帧扩展-TKinter
我正在构建一个为内部数据文件提供视口的应用程序。这些文件有点复杂,所以我创建了三个子窗口来处理文件的不同方面。左上角提供了不同部分的概要,右上角提供了一系列包含在数据文件中发现的错误的文本小部件,而左下角提供了数据文件本身的视图。为了方便所有这些,我编写了一个小类,作为每个部分的框架,可以用标签、文本框等填充(代码如下) 我遇到的问题是,右上角和下半部分的文本小部件不随其包含的框架展开。基于对effbot.org、Stackoverflow和其他网站的各种搜索,我认为我的设置是正确的,但显然有些地方出了问题。如果我放大主窗口,每个部分都会进行相应的调整,但是文本窗口小部件不会从左向右展开以填充新的子窗口维度 任何提示都非常感谢 下面是为子窗口提供功能的类:Python 文本框不随包含的帧扩展-TKinter,python,tkinter,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))
框架还必须设置扩展和填充选项(您必须检查ScrollingChildFrame的功能,这不是对代码不完整的抱怨,只是指出下一步)。在以下代码中对框架使用just 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()
“文本框”不是Tkinter常用的术语。如果您使用“文本小部件”或“输入小部件”而不是“文本框”,您的描述将更容易理解。请阅读有关发布最小示例的内容。文本框和文本框不会更改为文本小部件。至于最小的例子,我试图尽可能地减少这一点,以使问题得到解决。我有一种感觉,如果我进一步减少它,它将没有意义,我会因为没有提供足够的示例代码而受到批评。啊哈!这是有道理的。非常感谢。我现在正致力于在代码中实现。一旦我让它在我这边起作用,我会投票给这一个作为答案。
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()