Resizing Treeview with scrollbar in paned window does not work

Viewed 169

Goal:

  • Paned frame / window with a Treeview that has a scrollbar
  • Resizing the frame should resize the Treeview and maintain the scrollbar visible

I have tried the following code:

    # set paned Windows layout, main stacked horizontally, left stacked vertically
    self.pwMain = PanedWindow(orient=tk.HORIZONTAL)
    self.pwMain.grid(sticky=tk.NSEW)
    self.pwLeft = PanedWindow(orient=tk.VERTICAL)
    self.pwLeft.grid(sticky=tk.NSEW)
    
    # set editorText
    self.editorText = ScrolledText(
        self, wrap=tk.WORD, undo=True, relief=tk.FLAT)
    self.editorText.grid(sticky=tk.NSEW)
    self.pwLeft.add(self.editorText)
    
    # set AutoCompleteList Tree
    self.style = ttk.Style()  # remove borders from TreeView
    self.style.layout(
        "Treeview", [('Treeview.treearea', {'sticky': 'nswe'})])
    # only show headings columns, not root column of tree
    self.autoCompleteList = Treeview(
        self, columns=2, show=["headings"], selectmode='browse')
    self.autoCompleteList["columns"] = ("#1", "#2")
    self.autoCompleteList.heading('#1', text='Record Name')
    self.autoCompleteList.heading('#2', text='Record ID')
    # Specify attributes of the columns (We want to stretch it!)
    self.autoCompleteList.column('#1', stretch=tk.YES)
    self.autoCompleteList.column('#2', stretch=tk.YES)
    # autocomplete vertical scrollbar
    self.aclvbar = AutoScrollbar(self.autoCompleteList, orient=tk.VERTICAL, command=self.autoCompleteList.yview)
    self.autoCompleteList.configure(yscrollcommand=self.aclvbar.set)
    self.autoCompleteList.grid(sticky=tk.NSEW)
    self.aclvbar.grid(row=0, column=1, sticky='ns')
    self.pwLeft.add(self.autoCompleteList)

The Scrollbar is displayed like this

I also tried fitting the Treeview & Scrollbar in a separate frame (using grid), which results in the scrollbar being displayed as expected (right of the Treeview). The problem with this approach is that the Treeview is not resized anymore when dragging the sash of the paned window.

1 Answers

The scrollbar shouldn't be a child of the treeview. The treeview and scrollbar should share the same parent.

Related