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

时间:2016-11-09 21:10:04

标签: python tkinter

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

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

如何制作以使包含窗口/框架扩展以始终适合内容?我想继续使用网格管理器进行组织。

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

WIndows 10上的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()

0 个答案:

没有答案
相关问题