[Tutor] Expanding a frame into an expanding window

Phil phillor9 at gmail.com
Mon Mar 25 03:07:40 EDT 2024


Thank you for reading this.

I've created a custom widget that I'm importing into an app.  The app 
includes 3 frame classes including the imported custom widget class. The 
widgets on those frames all use the grid method.

When the app window is expanded the frames expand and frame's widgets 
move except for the frame that uses the imported class. That frame 
expands but the widgets on that frame remain in place. So there must be 
a problem with the custom widget class.

The following are 2 simplified versions of the custom widget class to 
illustrate the problem.

The version that uses the pack method does expand correctly but, of 
course, the two widgets are under each other rather than beside each 
other.  The frame that uses the imported class also expands but the two 
widgets do not move.

import tkinter as tk


class SimpleEntry(tk.Frame):
     def __init__(self, parent, bg_colour="light blue"): # default 
colour is light blue
         super().__init__(parent, bg=bg_colour)

         self.enter_label = tk.Label(self, text="enter a number")
         self.number_enter = tk.Entry(self)

         self.enter_label.grid(row=0, column=0, padx=5, pady=5, 
sticky="nsew")
         self.number_enter.grid(row=0, column=1, padx=5, pady=5, 
sticky="nsew")

'''

import tkinter as tk


class SimpleEntry(tk.Frame):
     def __init__(self, parent, bg_colour="light blue"):
         super().__init__(parent, bg=bg_colour)

         self.enter_label = tk.Label(self, text="enter a number")
         self.number_enter = tk.Entry(self)

         self.enter_label.pack(expand=True)
         self.number_enter.pack(expand=True)

'''

I've spent all day on this and all I've achieved is a headache.

-- 
Regards,
Phil



More information about the Tutor mailing list