Search code examples
pythontkintertkinter-scrolledtext

How to make flexible widget from changing the window size?


I want to make my scrolledtext to change it's size with the window size

# import GUI
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext


# create instance
win = tk.Tk()

# title name
win.title("Notepad ver.1.0")

# resizable option
win.resizable(True, True)

# geometry
win.geometry("1900x1000")

# frame 1
frame1 = ttk.LabelFrame(win, text='')
frame1.grid(column=0, row=0)
# some lables are inside frame1
ttk.Label(frame1, text='Hello World').grid(column=0, row=0)


# frame2
frame2 = ttk.LabelFrame(win, text='')
# set the width and height of your frame as desired using ipadx and ipady
frame2.grid(column=0, row=7, sticky=tk.S, ipadx=880, ipady=370)
# disable grid propagation to prevent resizing on font size changes
frame2.grid_propagate(False)

# scrolled text
ScrWidth = 0
ScrHight = 0
scr = scrolledtext.ScrolledText(frame2, wrap=tk.WORD, width=ScrWidth, height=ScrHight)
scr.pack(side='left', fill="both", expand=True, ipadx=0, ipady=0)
scr.config(font='Arial, 10')
scr.grid_propagate(False)

# start GUI
win.mainloop()

I tried to ask ChatGpt to do it and also google it and found that

win.bind("<Configure>", function)

will make my code work but it made my code more glitchy.

The glitch: The Glitch

Thank You for everyone for helping me!


Solution

  • If you want the instance of ScrolledText (scr) to be resized whenever the window is resized, you need to:

    • configure its parent frame2 to use all the available space by setting sticky="nsew" in frame2.grid(...)
    • allocate all available space to row 7 and column 0 where frame2 is put by calling win.rowconfigure(7, weight=1) and win.columnconfigure(0, weight=1)

    Below is the updated code:

    # import GUI
    import tkinter as tk
    from tkinter import ttk
    from tkinter import scrolledtext
    
    
    # create instance
    win = tk.Tk()
    
    # title name
    win.title("Notepad ver.1.0")
    
    # resizable option
    win.resizable(True, True)
    
    # geometry
    win.geometry("1900x1000")
    
    # frame 1
    frame1 = ttk.LabelFrame(win, text='')
    frame1.grid(column=0, row=0)
    # some lables are inside frame1
    ttk.Label(frame1, text='Hello World').grid(column=0, row=0)
    
    win.rowconfigure(7, weight=1)
    win.columnconfigure(0, weight=1)
    
    # frame2
    frame2 = ttk.LabelFrame(win, text='')
    # set the width and height of your frame as desired using ipadx and ipady
    ### changed sticky option to "nsew"
    frame2.grid(column=0, row=7, sticky="nsew", ipadx=880, ipady=370)
    # disable grid propagation to prevent resizing on font size changes
    #frame2.grid_propagate(False)  # it is not necessary
    
    # scrolled text
    ScrWidth = 0
    ScrHight = 0
    scr = scrolledtext.ScrolledText(frame2, wrap=tk.WORD, width=ScrWidth, height=ScrHight)
    scr.pack(side='left', fill="both", expand=True, ipadx=0, ipady=0)
    scr.config(font='Arial, 10')
    #scr.grid_propagate(False)  # it is not necessary
    
    # start GUI
    win.mainloop()