[Tutor] Expanding a frame into an expanding window

Alan Gauld alan.gauld at yahoo.co.uk
Mon Mar 25 20:58:12 EDT 2024


On 25/03/2024 22:57, Phil wrote:

>> Thank you Alan for your reply and I appreaciate it geatly. 
> The problem only occures when the class SimpleEntry is imported. 
> The ButtonFrame widget moves but the EnrtyFrame widget (SimpleEntry) does not move.


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.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")

> class EntryFrame(tk.Frame):
>      def __init__(self, parent):
>          super().__init__(parent)
>          self.entry_widget = SimpleEntry(self, bg_colour="yellow")
>          self.entry_widget.pack(fill="both", expand=True)
> 
> 
> class ButtonFrame(tk.Frame):
>      def __init__(self, parent):
>          super().__init__(parent, bg="light green")
>          self.add_button = tk.Button(self, text="Press me", 
>                                      command=self.on_button_click)
>          self.add_button.pack(fill="both", expand=True)
> 
>      def on_button_click(self):
>          pass
> 
> 
> class App(tk.Tk):
>      def __init__(self):
>          super().__init__()
>          self.title("Simple Class Test")
> 
>          self.entry_frame = EntryFrame(self)
>          self.button_frame = ButtonFrame(self)
> 
>          self.entry_frame.grid(row=0, column=0,                                  padx=5, pady=5, sticky="nsew")
>          self.button_frame.grid(row=1, column=0, 
                                  padx=5, pady=5, sticky="nsew")
> 
>          self.grid_rowconfigure(0, weight=1)
>          self.grid_rowconfigure(1, weight=1)
>          self.grid_columnconfigure(0, weight=1)

Notice that you set the frames here to have weight=1
That means these two frames expand when resized.
But notice that inside the SimpleEntry frame you do not
set the weights to 1 so they use the default of zero
and do not expand.

Adding

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

to the SimpleEntry class should resolve things

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list