Make stretchable split screen in Tkinter

6.6k views Asked by At

I want to make a window which enables split text screens in Tkinter. I also want to be able to "stretch" the screens with the mouse, so for example if I want one of the screens to be temporarily bigger than the other, I just drag it with the mouse.

I thought I could put a Text widget inside a PanedWindow widget since I thought the PanedWindow widget always is stretchable, but my code doesn't quite do the job. I'm able to get split screens, but they aren't stretchable. Here Is my (Unnecessary long but simple) code so far:

from Tkinter import *
root = Tk()

# Seems strange to column- and rowconfigure the root but if I don't -
# the text widgets won't resize at all
for i in range(4):
    root.columnconfigure(0, weight=1)
for i in range(1,3):
    root.rowconfigure(1, weight=1)

# make a master PanedWindow
m1 = PanedWindow(root)
m1.grid(column=0, row=0, rowspan=4, columnspan=4, sticky=E+N+W+S)
for i in range(4):
    m1.columnconfigure(i, weight=1) # Enable vertical resizing
for i in range(1,3):
    m1.rowconfigure(i, weight=1) #Enable horizontal resizing

# make a PanedWindow inside m1, positioned to the left
m2=PanedWindow(m1)
m2.grid(column=0, row=1, columnspan=2, rowspan=2, sticky=E+N+W+S)
for i in range(2):
    m2.columnconfigure(i, weight=1) # Enable vertical resizing
for i in range(1,3):
    m2.rowconfigure(i, weight=1) #Enable horizontal resizing

# make another PanedWindow inside m1, positioned to the right
m3=PanedWindow(m1)
m3.grid(column=2, row=1, columnspan=2, rowspan=2, sticky=E+N+W+S)
for i in range(2, 4):
    m3.columnconfigure(i, weight=1) # Enable vertical resizing
for i in range(1,3):
    m3.rowconfigure(i, weight=1) #Enable horizontal resizing

# Add a text widget in m2
text1 = Text(m2, height=15, width =15)
m2.add(text1) 

# Add another textwidget in m3
text2=Text(m3, height=15, width=15)
m3.add(text2) 

root.mainloop()
2

There are 2 answers

1
Bryan Oakley On BEST ANSWER

The main problem with your code is that you're not using PanedWidnow correctly. For example, you can't pack or grid one PanedWindow inside another. To place one widget inside a PanedWindow you must use the paned window .add() method. So, to put m2 inside m1 you must do m1.add(m2). Treat a PanedWindow like a Frame, and .add() is the equivalent to .pack() or .grid().

Also, it seems like you're thinking an PanedWindow is a pane, which it is not. If you want three panes for three side-by-side windows, you only need to create a single instance of PanedWindow, and then call .add(...) three times, once for each child window. While you can put paned windows inside paned windows, it's rarely the right thing to do unless one is horizontal and the other is vertical. For most circumstances, a single instance of PanedWindow is all you need.

4
fhdrsdg On

You're making this way too complicated. Just following the first example here, I made what you want:

from Tkinter import *
root = Tk()

m = PanedWindow(root)
m.pack(fill=BOTH, expand=1)

text1 = Text(m, height=15, width =15)
m.add(text1) 

text2=Text(m, height=15, width=15)
m.add(text2) 

root.mainloop()