0

I have run in to a diffuctly where despite using the sticky and weight options from the grid and columnconfigure methods respectively, I am unable to make the column expand the entire width of my frame.

I have read through this post which explains things well, however I haven't had any luck. Below is a simplified example of my code, where the desired result can be achieved by uncommenting the lines using the pack method, which I'd like to replicate using the grid method as well.

import tkinter as tk

class myGUI(tk.Frame):

    class ScrollableFrame(tk.Frame):
        
        def __init__(self, parent):
            
            # Create scrollbar
            self.frame = tk.Frame(parent)
            self.canvas = tk.Canvas(self.frame)
            self.yscrollbar = tk.Scrollbar(self.canvas, orient='vertical', command=self.canvas.yview)
            tk.Frame.__init__(self, self.canvas)
            self.canvas.create_window((0, 0), window=self, anchor='nw')

            # Configure scrollbar
            self.canvas.configure(yscrollcommand=self.yscrollbar.set)
            self.canvas.bind('<Configure>', lambda e: self.canvas.configure(scrollregion=self.canvas.bbox('all')))

            # Pack scrollbar
            self.canvas.pack(side='left', fill='both', expand=True)
            self.yscrollbar.pack(side='right', fill='y')

            # Configure placement
            self.pack = self.frame.pack
            self.place = self.frame.place
            self.grid = self.frame.grid

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.wrap = tk.LabelFrame(parent)
        self.myFrame = self.ScrollableFrame(parent)

        for i in range(50):
            tk.Button(self.myFrame, text='My Button - '+str(i)).pack()

        # self.myFrame.pack(fill='both', expand='yes')
        # self.wrap.pack(fill='both', expand='yes')
        self.myFrame.grid(row=0, column=0, sticky = 'news')
        self.wrap.grid(row=1, column=0, sticky = 'news')
        parent.columnconfigure(0, weight=1)
        parent.columnconfigure(1, weight=1)
        parent.rowconfigure(0, weight=1)
        parent.rowconfigure(1, weight=1)

if __name__ == "__main__":
    root = tk.Tk()

    root.geometry('500x500')
    root.title('Scrollbar Test')
    myGUI(root)

    root.mainloop()
kostas1335
  • 59
  • 1
  • 6
  • Not related to your question: defining classes inside other classes is bad practice and should be avoided. – TheLizzard Sep 08 '21 at 19:20
  • Good to know, you had commented on my previous post to include the scrollable frame code within a class and that's how I understood it. Will place the class outside the other one on my actual code. – kostas1335 Sep 08 '21 at 19:27
  • To my knowledge it isn't possible to make widgets inside a scrolled canvas fill the width of the canvas. Try adding `width=27` to the `tk.Button(...)`. That will set the width of the buttons – TheLizzard Sep 08 '21 at 19:37
  • I don't want the widgets inside the scrolled canvas to fill its width, but rather the scrolled canvas itself to fill the width of the main application window. – kostas1335 Sep 08 '21 at 19:39
  • 1
    Try removing the `parent.columnconfigure(1, weight=1)`. Right now you aren't using the second column and that line forces the width to be split evenly between the 0th and 1st columns – TheLizzard Sep 08 '21 at 19:42
  • ^^ yep, just realised my mistake... – kostas1335 Sep 08 '21 at 19:45

0 Answers0