Aardhyn
Aardhyn

Reputation: 113

Paned Window Resizing Issues

I've been trying out panedWindows for my app layout, but I'm having issues when the window resizes. This is the layout I've started out with: initial layout

The panes drag as I want them too, but I want to specify what panes expand to fill the root window, and which ones stay a specific width. For example, when I drag the window to the right or left, the pane on the expanding size extends to fill the extra space. I want only the centre pane to expand, and the right and left only when the sash is dragged:

what happens when resizing window

like this:

desired behaviour when resizing window

When the panes were managed with the pack geometry manager, I could tell the edge panes to stay a specific width, and tell the centre pane to expand. How can I achieve a similar layout with paned windows?

Heres a minimal working example:

import tkinter

root = tkinter.Tk()
root.geometry("480x200+200+200")
root.title("Paned Window Example")

main = tkinter.PanedWindow()

main.pack(expand = 1, fill = "both")

pane1 = tkinter.Frame(width = 90, bg = "red")
pane2 = tkinter.Frame(width = 310, bg = "blue")
pane3 = tkinter.Frame(width = 90, bg = "red")

for pane in pane1, pane2, pane3 : main.add(pane)

root.mainloop()

Upvotes: 2

Views: 1426

Answers (1)

j_4321
j_4321

Reputation: 16179

You can use the stretch option when adding the panes to the PanedWindow. It takes the following values: 'always', 'first', 'last', 'middle', and 'never'. You can get a more complete description in the tcl/tk documentation.

In your case, use 'never' for the side panes and 'always' for the middle one:

import tkinter

root = tkinter.Tk()
root.geometry("480x200+200+200")
root.title("Paned Window Example")

main = tkinter.PanedWindow()

main.pack(expand=True, fill="both")

pane1 = tkinter.Frame(width=90, bg="red")
pane2 = tkinter.Frame(width=310, bg="blue")
pane3 = tkinter.Frame(width=90, bg="red")

main.add(pane1, stretch='never')
main.add(pane2, stretch='always')
main.add(pane3, stretch='never')

root.mainloop()

Note that the above solution works only for tkinter.PanedWindow and not for ttk version of the widget. The .add() method of ttk.PanedWindow accepts only one option: weight. But we can achieve the same result by setting the side panes' weight to 0 and the middle pane's to 1:

import tkinter
from tkinter import ttk

root = tkinter.Tk()
root.geometry("480x200+200+200")
root.title("Paned Window Example")

main = ttk.PanedWindow(orient='horizontal')

main.pack(expand=True, fill="both")

pane1 = tkinter.Frame(width=90, bg="red")
pane2 = tkinter.Frame(width=310, bg="blue")
pane3 = tkinter.Frame(width=90, bg="red")

main.add(pane1, weight=0)
main.add(pane2, weight=1)
main.add(pane3, weight=0)

root.mainloop()

Upvotes: 5

Related Questions