Search code examples
pythontkintertkinter-layoutttkbootstrap

TTKBootstrap /Tkinter Text Widget Resizing my Frames


I coded a simple ttkbootstrap/tkinter application that contains 4 Frames that divides the "root" window in 4 and added a simple button and a Text widget.

Here's the code:

import ttkbootstrap as ttk

root = ttk.Window(themename="darkly")
root.geometry("800x600")
root.columnconfigure([0,1,2,3,4,5,6,7,8,9], weight= 1, uniform="a")
root.rowconfigure([0,1,2,3,4,5,6,7,8,9], weight= 1, uniform="a")

#Creating Frames
frame1 = ttk.Frame(root, style="dark")
frame1.grid(row=0, column=0, columnspan=5, rowspan=5,sticky="nswe")
frame1.columnconfigure([0,1,2,3,4,5], weight=1, uniform="a")
frame1.rowconfigure([0,1,2,3,4,5], weight=1, uniform="a")

frame2 = ttk.Frame(root, style="light")
frame2.grid(row=0, column=5, columnspan=5,rowspan=5, sticky="nswe")
frame2.columnconfigure([0,1,2,3,4,5], weight=1, uniform="a")
frame2.rowconfigure([0,1,2,3,4,5], weight=1, uniform="a")

frame3 = ttk.Frame(root, style="light")
frame3.grid(row=5, column=0, columnspan=5, rowspan=5, sticky="nswe")
frame3.columnconfigure([0,1,2,3,4,5], weight=1, uniform="a")
frame3.rowconfigure([0,1,2,3,4,5], weight=1, uniform="a")

frame4 = ttk.Frame(root, style="dark")
frame4.grid(row=5, column=5, columnspan=5, rowspan=5, sticky="nswe")
frame4.columnconfigure([0,1,2,3,4,5], weight=1, uniform="a")
frame4.rowconfigure([0,1,2,3,4,5], weight=1, uniform="a")

#Widgets
start_button = ttk.Button(frame1, text="Start", bootstyle="default", command=start_app)
start_button.grid(row=1, column=0, columnspan=2, sticky="nswe")

texto = ttk.Text(root, wrap="word",width=1,height=1, font=("Helveltica", 12))
texto.grid(row=2, column=0, columnspan=3,rowspan=1 ,sticky="nswe")
texto.insert(index=ttk.END, chars="hello world")

scroll_y = ttk.Scrollbar(root, orient=ttk.VERTICAL, command=texto.yview, style="light")
scroll_y.grid(row=2, column=3,rowspan=1,sticky="wsn")
texto.config(yscrollcommand=scroll_y)
texto["yscrollcommand"] = scroll_y.set

root.mainloop()

Looks good.

Looks normal

But when I set the master of the texto instance to frame1, the whole layout gets messed up.

Messed layout

This is the code after changes

texto = ttk.Text(frame1, wrap="word",width=1,height=1, font=("Helveltica", 12))
texto.grid(row=2, column=0, columnspan=3,rowspan=1 ,sticky="nswe")
texto.insert(index=ttk.END, chars="hello world")

scroll_y = ttk.Scrollbar(frame1, orient=ttk.VERTICAL, command=texto.yview, style="light")
scroll_y.grid(row=2, column=3,rowspan=1,sticky="wsn")
texto.config(yscrollcommand=scroll_y)
texto["yscrollcommand"] = scroll_y.set

Does anyone know why this happens, and how to fix it? I need them to stop resizing. Even when I set the master to root it can produce some strange behaviour, and screw all the frame positioning.

Tried changing the master to frame1 and my layout got messy and uneven. I need them to stop resizing.


Solution

  • How I understand it you have to think as each frame as an individual layer. Hou have the root layer on which you want to add four frames: 2x2 (row and columns: [0,1]) inside the frames you are starting from new to count. Try the following code: Ijust added the start_app command so that there is no error with the texto command...

    import ttkbootstrap as ttk
    
    def start_app():
        pass
    
    
    root = ttk.Window(themename="darkly")
    root.geometry("800x600")
    root.columnconfigure([0,1], weight= 1, uniform="a")
    root.rowconfigure([0,1], weight= 1, uniform="a")
    
    #Creating Frames
    frame1 = ttk.Frame(root, style="dark")
    frame1.grid(row=0, column=0, sticky="nswe")
    frame1.columnconfigure([0,1,2,3,4,5], weight=1, uniform="a")
    frame1.rowconfigure([0,1,2,3,4,5], weight=1, uniform="a")
    
    frame2 = ttk.Frame(root, style="light")
    frame2.grid(row=0, column=1, sticky="nswe")
    frame2.columnconfigure([0,1,2,3,4,5], weight=1, uniform="a")
    frame2.rowconfigure([0,1,2,3,4,5], weight=1, uniform="a")
    
    frame3 = ttk.Frame(root, style="light")
    frame3.grid(row=1, column=0, sticky="nswe")
    frame3.columnconfigure([0,1,2,3,4,5], weight=1, uniform="a")
    frame3.rowconfigure([0,1,2,3,4,5], weight=1, uniform="a")
    
    frame4 = ttk.Frame(root, style="dark")
    frame4.grid(row=1, column=1, sticky="nswe")
    frame4.columnconfigure([0,1,2,3,4,5], weight=1, uniform="a")
    frame4.rowconfigure([0,1,2,3,4,5], weight=1, uniform="a")
    
    #Widgets
    start_button = ttk.Button(frame1, text="Start", bootstyle="default", command=start_app)
    start_button.grid(row=1, column=0, columnspan=2, sticky="nswe")
    
    texto = ttk.Text(frame1, wrap="word",width=1,height=1, font=("Helveltica", 12))
    texto.grid(row=2, column=0, columnspan=3,rowspan=1 ,sticky="nswe")
    texto.insert(index=ttk.END, chars="hello world")
    
    scroll_y = ttk.Scrollbar(frame1, orient=ttk.VERTICAL, command=texto.yview, style="light")
    scroll_y.grid(row=2, column=3,rowspan=1,sticky="wsn")
    texto.config(yscrollcommand=scroll_y)
    texto["yscrollcommand"] = scroll_y.set
    
    root.mainloop()