Python 如何使用tkinter网格使包含的窗口/框架展开以适应内容?

Python 如何使用tkinter网格使包含的窗口/框架展开以适应内容?,python,tkinter,Python,Tkinter,在下面的代码中(复制并粘贴到文本文件中,您应该能够立即运行它),文本被输入到条目小部件中,然后显示在多列列表框中。列表框的列将展开以适合文本,但包含的窗口/框架不会展开以适合展开的列表框 试着输入一个短字符串“test”,然后输入一个很长的字符串“qwertyuiopasdf”,看看我的意思 如何使包含的窗口/框架扩展以始终适合内容?我想继续使用网格管理器,因为组织是可能的 多列列表框改编自,主应用程序类改编自 Windows10上的Python 3.5.1。Tkinter版本8.6 from

在下面的代码中(复制并粘贴到文本文件中,您应该能够立即运行它),文本被输入到条目小部件中,然后显示在多列列表框中。列表框的列将展开以适合文本,但包含的窗口/框架不会展开以适合展开的列表框

试着输入一个短字符串“test”,然后输入一个很长的字符串“qwertyuiopasdf”,看看我的意思

如何使包含的窗口/框架扩展以始终适合内容?我想继续使用网格管理器,因为组织是可能的

多列列表框改编自,主应用程序类改编自

Windows10上的Python 3.5.1。Tkinter版本8.6

from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont

class McListBox(ttk.Treeview):
    #use a ttk.Treeview as a multicolumn ListBox
    def __init__(self, master=None, **kwargs):
        headers=kwargs['columns']
        id=kwargs['name']
        ttk.Treeview.__init__(self, master, name=id, columns=headers, show='headings')
        self.headers = headers
        self.items = []
        self.build_tree()

    def build_tree(self):
        # set up the columns
        for col in self.headers:
            self.heading(col, text=col.title())
            # adjust the column's width to the header string
            self.column(col, width=tkFont.Font().measure(col.title()))
        # in case items are added already
        for item in self.items:
            self.insert('', 'end', values=item)
            # adjust column's width if necessary to fit each value
            for ix, val in enumerate(item):
                col_w = tkFont.Font().measure(val)
                if self.column(self.headers[ix],width=None)<col_w:
                    self.column(self.headers[ix], width=col_w)

    def add(self, text):
        # add the text in the first column and a 1 in the second
        item = (text, 1)
        self.insert('', 'end', values=item)
        # adjust column's width if necessary to fit each value
        for ix, val in enumerate(item):
            col_w = tkFont.Font().measure(val)
            if self.column(self.headers[ix],width=None)<col_w:
                self.column(self.headers[ix], width=col_w)

class Application(ttk.Frame):
    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)
        self.master.rowconfigure(0, weight=1)
        self.master.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=1)
        self.columnconfigure(1, weight=1)
        self.grid(row=0,column=0,sticky='nsew')
        self.createWidgets()

    def createWidgets(self):
        # this is the multi column list box
        varDisplayArea = McListBox(self, name='varDisplayArea', columns=['var', 'val'])
        varDisplayArea.grid(column=0, row=0, sticky='nsew')
        # this is the input to go into the multicolum list box
        inputBox = ttk.Entry(self, name='inputBox')
        inputBox.grid(column=0, row=1, sticky=W)
        inputBox.focus()
        self.master.bind('<Return>', self.addEntry)

    def addEntry(self, *args):
        # the value in the entry widget is added to the list box
        inputBox = self.children['inputBox']
        varDisplayArea = self.children['varDisplayArea']
        input = inputBox.get()
        inputBox.delete(0, END)
        varDisplayArea.add(input)
        inputBox.focus()

app = Application()
app.master.title('sample app')
app.mainloop()
从tkinter导入*
从tkinter导入ttk
将tkinter.font作为tkFont导入
类McListBox(ttk.Treeview):
#将ttk.Treeview用作多列列表框
定义初始化(自,主=无,**kwargs):
标题=kwargs[“列”]
id=kwargs['name']
ttk.Treeview.\uuuu init\uuuuu(self,master,name=id,columns=headers,show='headers'))
self.headers=标题
self.items=[]
self.build_树()
def生成树(自):
#设置列
对于self.header中的列:
self.heading(col,text=col.title())
#将列的宽度调整为标题字符串
self.column(col,width=tkFont.Font().measure(col.title()))
#如果已经添加了项目
对于self.items中的项目:
self.insert(“”,'end',value=item)
#如有必要,调整列的宽度以适应每个值
对于ix,枚举中的val(项目):
col_w=tkFont.Font().measure(val)
如果self.column(self.headers[ix],width=None)