Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create expanding PanedWindow with gridlayout in tkinter?

Basically I want to recreate this code with gridlayout:

from tkinter import *

m1 = PanedWindow()
m1.pack(fill=BOTH, expand=1)

left = Label(m1, text="left pane")
m1.add(left)

m2 = PanedWindow(m1, orient=VERTICAL)
m1.add(m2)

top = Label(m2, text="top pane")
m2.add(top)

bottom = Label(m2, text="bottom pane")
m2.add(bottom)

mainloop()

My code looks like this:

from tkinter import *
from tkinter import ttk

root = Tk()

root.title("PanedWindows")
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)

left_right_pane = PanedWindow(root, orient=HORIZONTAL, relief='groove', borderwidth=2)
left_right_pane.grid(column=0, row=0, sticky=(W,S,E,N), columnspan=2)
left_right_pane.rowconfigure(0, weight=1)
left_right_pane.columnconfigure(0, weight=1)
left_right_pane.rowconfigure(1, weight=1)
left_right_pane.columnconfigure(1, weight=1)

leftframe = ttk.Frame(root, relief='groove', borderwidth=2)
leftframe.grid(column=0, row=0, sticky=(N, W, E, S), rowspan=2)
leftframe.rowconfigure(0, weight=1)
leftframe.columnconfigure(0, weight=1)
left_right_pane.add(leftframe)

top_bottom_pane = PanedWindow(root, orient=VERTICAL)
top_bottom_pane.grid(column=1, row=0, rowspan=2, sticky=(N, W, E, S))
top_bottom_pane.rowconfigure(0, weight=1)
top_bottom_pane.columnconfigure(0, weight=1)
left_right_pane.add(top_bottom_pane)

upperframe = ttk.Frame(root, relief='groove', borderwidth=2)
upperframe.grid(column=1, row=0, sticky=(N, W, E, S))
upperframe.columnconfigure(1, weight=1)
upperframe.rowconfigure(0, weight=1)
top_bottom_pane.add(upperframe)

bottomframe = ttk.Frame(top_bottom_pane, relief='groove', borderwidth=2)
bottomframe.grid(column=1, row=1, sticky=(N, W, E, S))
bottomframe.columnconfigure(1, weight=1)
bottomframe.rowconfigure(1, weight=1)
top_bottom_pane.add(bottomframe)

entry = ttk.Entry(leftframe)
entry.grid(column=0, row=0, sticky=(W,S,E,N))

top = Label(upperframe, text="top pane")
top.grid(column=1, row=0, sticky=(W,S,E,N))

bottom = Label(bottomframe, text="bottom pane")
bottom.grid(column=1, row=1, sticky=(W,S,E,N))

mainloop()

The code got a little out of hand since I started experimenting with all kinds solutions, basically I stickied every element to every direction and added weight to every row and column and still it refuses to work. It seems like tkinter treats my panedwindows as single cell so when ever I expand them, only 1 element gets expanded inside them, and I'd like all of the elements to be equally expanded.

like image 259
DisturbedTK Avatar asked Dec 24 '14 12:12

DisturbedTK


1 Answers

Your question has two parts, how to replace pack with grid, and how to get the panes of a Panedwindow to grow and shrink equally. I'll address these two parts of your question separately.

Replacing pack with grid

Starting with your original code, replace this:

m1.pack(fill=BOTH, expand=1)

with this:

root = Tk()

m1.grid(row=0, column=0, sticky ="nsew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

Getting panes to grow/shrink equally

To get all of the panes to grow equally, that is completely independent of whether you use grid or pack. The growing and shrinking is controlled by the paned window, not by the geometry manager. In effect, panedwindow is another geometry manager.

If you want all panes to grow/shrink equally, add the option stretch="always" when adding each child, eg:

m1.add(left, stretch="always")
m1.add(m2, stretch="always")
m2.add(top, stretch="always")
m2.add(bottom, stretch="always")

Note: the other options for stretch are "first", "last", "middle" or "never". The definitive documentation is in the tcl/tk man page, and only requires slight transformations to work with tkinter. See http://tcl.tk/man/tcl8.5/TkCmd/panedwindow.htm#M32

like image 139
Bryan Oakley Avatar answered Nov 15 '22 08:11

Bryan Oakley