简体   繁体   中英

tkinter: treeview bigger than grid cell

I placed the treeview widget within a grid cell, which uses stick=N+S+E+W , in order to attach the widget borders to the grid cell's borders. Also, I set grid_columnconfigure(0, weight=1) for the parent (root), in order to resize widgets in column 0 (the one which also contains the treeview) according to the main window size (resizable). Despite so, some columns ("bitrate", "status") of the treeview widget do not show, because the whole widget is bigger than the grid cell which contains it. Here's the source:

from tkinter import *
from tkinter.ttk import *

class MainWindow(Frame):

    def __init__(self, parent):

        Frame.__init__(self, parent)   
        self.parent = parent        
        self.buildUI()

    def buildUI(self):

        # window title

        self.parent.title("test")

        # window config

        self.parent.minsize(width=600, height=320)
        self.parent.grid_columnconfigure(0, weight=1)
        self.parent.rowconfigure(1, weight=1)
        self.parent.geometry("600x320")

        # menu bar (Settings, About, Exit)

        self.menubar = Menu(self.parent)
        self.filemenu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="File", menu=self.filemenu)

        self.filemenu.add_command(label="Settings")
        self.filemenu.add_command(label="About")
        self.filemenu.add_separator()
        self.filemenu.add_command(label="Exit", command=self.parent.quit)

        self.parent.config(menu=self.menubar)

        # entry search

        self.entrySearch = Entry(self.parent)
        self.entrySearch.grid(row=0, column=0, sticky=W+E)

        # button search

        self.buttonSearch = Button(text="Search")
        self.buttonSearch.grid(row=0, column=1)

        # results table

        self.resultsTable = Treeview(self.parent)
        self.resultsTable["columns"] = ("title", "artist", "length", "bitrate", "status")
        self.resultsTable["show"] = "headings" # remove first empty column (id)
        self.resultsTable.heading("title", text="Title")
        self.resultsTable.heading("artist", text="Artist")
        self.resultsTable.heading("length", text="Length")
        self.resultsTable.heading("bitrate", text="Bitrate")
        self.resultsTable.heading("status", text="Status")
        self.resultsTable.grid(row=1, column=0, columnspan=2, sticky=N+S+E+W)

        # bottom status bar

        self.statusBar = Label(self.parent, text="Ready")
        self.statusBar.grid(row=2, column=0, sticky=W)

def main():

    root = Tk()
    app = MainWindow(root)
    root.mainloop()  

if __name__ == '__main__':
    main()

Here's a screenshot: http://i.stack.imgur.com/U1g44.png . As you can see, the columns "bitrate" and "status" aren't shown. They can be viewed only by increasing the width of the main window. I can't figure out where's the problem. Thanks for taking your time.

The treeview widget has a method named column which lets you specify options for a given column. One of the options is stretch , which lets you determine whether the column should stretch and shrink when the widget is resized. You can also use the width attribute to specify the starting size. These two combined should cause your treeview to appear with all of the columns fitting on the screen.

self.resultsTable = Treeview(self.parent)
...
self.resultsTable.column("title", stretch=True, width=10)
self.resultsTable.column("artist", stretch=True, width=10)
self.resultsTable.column("length", stretch=True, width=10)
self.resultsTable.column("bitrate", stretch=True, width=10)
self.resultsTable.column("status", stretch=True, width=10)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM