Home > other >  Tkinter - Text and Scrolled bar widgets don't fill and fit the frame
Tkinter - Text and Scrolled bar widgets don't fill and fit the frame

Time:04-26

I created a custom Frame with two widgets (Text and Scrolledbar) already placed in it. when I use it the object works but the inside widgets don't follow the "grid" rules.

below my code:

import tkinter as tk
from tkinter import ttk

class TextFrame(tk.Frame):
    def __init__(self, container):
        super().__init__(container)

        # main frame:
        self.F1=tk.Frame(self, background="white", highlightbackground="#7a7a7a", highlightthickness=1)
        self.F1.pack(expand=True, fill=tk.BOTH)
        
        # text widget:
        self.T1=tk.Text(self.F1, wrap=tk.WORD, font=("Segoe UI", 9), border=0)
        self.T1.grid(padx=(0, 16), row=1, column=1, sticky="nsw")
        
        # scrolling bar:
        self.SB1=ttk.Scrollbar(self.F1, orient="vertical", command=self.T1.yview)
        self.SB1.grid(padx=(0, 1), pady=(1, 1), row=1, column=1, sticky="nse")
        self.T1.configure(yscrollcommand=self.SB1.set)

# main window:
class MainWindow:
    def __init__(self):

        self.parent=tk.Tk()
        self.parent.geometry("500x300 370 100")
        self.parent.title("Test")
        self.parent.configure(background="#f0f0f0")
        self.parent.minsize(400, 200)
        
        obj=TextFrame(self.parent)
        obj.pack(padx=(10,10), pady=(10,10), expand=True, fill=tk.BOTH)

        self.parent.mainloop()

if __name__=="__main__":
    app=MainWindow()

I just want that the widgets fill and fit the frame, and when I expand it, both of them have to expand too but it does't happen! why? how can I solve this issue?

enter image description here

CodePudding user response:

I think you need to add something like this for rows and columns. That way they are treated the same when scaling the window larger or smaller.

self.rowconfigure(0, weight=1), 
self.columnconfigure(0, weight=1)

CodePudding user response:

By default, grid rows and columns will grow or shrink to fit the size of their contents. If you want one or more rows or columns to expand to fill the window you must explicitly state that by setting the weight attribute of those rows or columns appropriately.

In the specific case of wanting one row and one column to expand, you need to set the weight of that row and that column to a positive integer. Since it's the only row or column you want to expand, any positive integer will do.

self.F1.grid_rowconfigure(0, weight=1)
self.F1.grid_columnconfigure(0, weight=1)

You also need the sticky attribute to be something like nsew (north, south, east, west) so that the contents of the row/column expand to fill the space given on all sides.

You further have the problem that you are putting the scrollbar and text widget in the same column. They need to be in separate columns. Also, while it's harmless to start counting rows and columns at 1, the first row and the first column are 0.

self.T1.grid(row=0, column=0, sticky="nsew")
self.SB1.grid(row=0, column=1, sticky="nsew")
  • Related