RRSC
RRSC

Reputation: 257

Issue in Entry Widget resizing in tkinter

I am using the following code to populate Entry widget on tkinter Frame:

import tkinter as tk

def populate(frame):
    '''Put in some fake data'''
    for row in range(100):
        tk.Label(frame, text="%s" % row, width=3, borderwidth="1", relief="solid").grid(row=row, column=0)
        tk.Entry(frame, width = 50).grid(row=row, column=1)
    
def onFrameConfigure(canvas):
    '''Reset the scroll region to encompass the inner frame'''
    canvas.configure(scrollregion=canvas.bbox("all"))
    
root = tk.Tk()
canvas = tk.Canvas(root, borderwidth=0)
frame = tk.Frame(canvas)
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((0,0), window=frame, anchor="nw")

frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

populate(frame)

root.mainloop()

Issue which I am facing in the above code is that when I am resizing the tkinter main window the Entry widget is not resizing itself automatically according to change in window size.

Can someone please help me out in solving this.

Upvotes: 1

Views: 415

Answers (1)

acw1668
acw1668

Reputation: 46669

If you want to resize the entries when the tkinter window is resized, you need to:

  • resize frame when canvas is resized
    def on_canvas_resized(event):
        canvas.itemconfig('frame', width=event.width)
    
    ...
    canvas.bind('<Configure>', on_canvas_resized)
    ...
    canvas.create_window((0,0), window=frame, anchor="nw", tag='frame') # added tag
    
  • make Entry to fill the available space horizontally
    def populate(frame):
        '''Put in some fake data'''
        for row in range(100):
            tk.Label(frame, text="%s" % row, width=3, borderwidth="1", relief="solid").grid(row=row, column=0)
            tk.Entry(frame, width = 50).grid(row=row, column=1, sticky='ew') # added sticky
    ...
    frame.columnconfigure(1, weight=1) # make column 1 to fill available space horizontally
    

Upvotes: 2

Related Questions