[Python] How to place tkinter PanedWindow sash correctly on a Notebook tab

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    Issue with PanedWindow Sash Placement in a Notebook Tab I am trying to create a Notebook tab that contains a PanedWindow with four panes. However, when I attempt to position the sashes manually, the third sash does not appear at the expected position.

    Expected Behavior The three sashes should be placed at positions 100, 657, and 1000.

    Observed Behavior

    • The first two sashes are placed correctly.
    • The third sash appears at 663 instead of 1000.

    Here is my code:


    import tkinter as tk
    from tkinter import ttk

    class Main:
    def __init__(self) -> None:
    self.root = tk.Tk()
    self.root.geometry('1100x400')
    self.root.protocol("WM_DELETE_WINDOW", self.dismiss)
    self.root.columnconfigure(0, weight=1)
    self.root.rowconfigure(0, weight=1)

    notebook = self._notebook(self.root)
    notebook.grid(row=0, column=0, sticky=tk.NSEW)

    self.root.mainloop()

    def _notebook(self, master: tk.Frame) -> ttk.Notebook:
    notebook = ttk.Notebook(master)
    self.test_tab = TestFrame(self, notebook)
    notebook.add(self.test_tab.master_frame, text="Test")
    return notebook

    def dismiss(self, *args) -> None:
    print([self.test_tab.master_frame.sash_coord(index) for index in range(3)])
    self.root.destroy()


    class TestFrame:
    def __init__(self, parent, master) -> None:
    self.root = parent.root
    self.master_frame = self._master_frame(master)
    self.master_frame.grid(row=0, column=0, sticky=tk.EW)

    # Expected sash positions
    sashes = [(100, 1), (657, 1), (1000, 1)]
    for index, (position, _) in enumerate(sashes):
    self.master_frame.sash_place(index, position, 0)

    # Debugging button to reattempt sash placement
    button = tk.Button(self.root, text="Check Sashes", command=self.click)
    button.grid(row=1, column=0)

    def _master_frame(self, master) -> ttk.PanedWindow:
    frame = tk.PanedWindow(master, orient=tk.HORIZONTAL)

    for _ in range(4):
    listbox = tk.Listbox(frame)
    frame.add(listbox)

    return frame

    def click(self, *args) -> None:
    # Attempt to reposition sashes again
    new_sashes = [(100, 1), (500, 1), (1000, 1)]
    for index, (position, _) in enumerate(new_sashes):
    self.master_frame.sash_place(index, position, 0)
    print([self.master_frame.sash_coord(index) for index in range(3)])

    if __name__ == "__main__":
    Main()




    Even after the window is created,on clicking the button, the sash will not get positioned correctly

    System Information:

    OS: Manjaro Linux
    KDE Plasma Version: 6.2.5
    KDE Frameworks Version: 6.10.0
    Qt Version: 6.8.2
    Python Version: 3.13.1
    tk.TkVersion: 8.6

    Any insights into why the third sash isn't placed correctly would be greatly appreciated!

    Continue reading...

Compartilhe esta Página