简体   繁体   English

tkinter:树视图大于网格单元格

[英]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. 我将treeview小部件放置在使用stick=N+S+E+W的网格单元中,以便将小部件边框附加到网格单元的边框。 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). 另外,我为父级(根)设置了grid_columnconfigure(0, weight=1) ,以便根据主窗口的大小(可调整大小)来调整第0列中的小部件(也包含树状视图)的大小。 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. 尽管如此,treeview小部件的某些列(“ bitrate”,“ status”)仍未显示,因为整个小部件大于包含它的网格单元。 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 . 这是屏幕截图: 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. treeview小部件具有一个名为column的方法,该方法可让您指定给定列的选项。 One of the options is stretch , which lets you determine whether the column should stretch and shrink when the widget is resized. 选项之一是stretch ,它使您可以确定在调整窗口小部件大小时是否应拉伸和收缩列。 You can also use the width attribute to specify the starting size. 您还可以使用width属性指定起始大小。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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