Make an expandable split screen in Tkinter

I want to create a window that allows text screens to be split 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 larger than the other, I just drag and drop it with the mouse.

I thought I could fit a Text widget inside a PanedWindow widget as I thought the PanedWindow widget is always extensible, but my code doesn't quite do the job. I can get split screens, but they are not expandable. Here is my (Unnecessary long but simple) code:

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()

      

+3


source to share


2 answers


The main problem with your code is that you are not using it PanedWidnow

correctly. For example, you cannot pack

or grid

one PanedWindow

inside the other. To place a single widget inside PanedWindow

you must use the paned window method .add()

. So, to put m2 inside m1 you have to do m1.add(m2)

. Treat a PanedWindow

like a Frame

, but it .add()

is equivalent to .pack()

or .grid()

.



It also seems that you are thinking that PanedWindow

- this is a panel that it is not. If you want three panes for three side-by-side windows, you only need to instantiate one PanedWindow

and then call .add(...)

three times, once for each child window. While you can paint windows with closed windows on windows, they rarely fit unless they are horizontal and others are vertical. For most cases, a single copy PanedWindow

is all you need.

+3


source


You are making it too hard. Just after the first example here , I did 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()

      

+2


source







All Articles