0

I tried to create a vertical scrolled frame for containing some widgets.

However, when I pack objects into it, the size of the frame fits to the max-width of some widget inside it.

How can I get it to stick to the right?

enter image description here

I've used the following code:

RadioList.py

class RadioList(Frame):
    def __init__(self, parent, *args, **kwargs):
        Frame.__init__(self, parent)
        self.pack()
        self._frame = Frame(self, *args, **kwargs)
        self._frame.pack(fill=BOTH, expand=YES)
        self._var = IntVar()
        # self._var = StringVar('a')

    def add(self, text, value):
        rb = Radiobutton(self._frame,
            text=text,
            value=value,
            variable=self._var,
            anchor=W,
            # width=200
            )
        # rb.grid()
        rb.pack(fill=X)

main.py:

class App(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        # self.geometry('400x300')
        self.grid()
        self.label = Label(self, text="hello world")
        self.label.grid()
        container = Frame(self)
        container.grid(row=1, columnspan=3)
        vframe = VerticalScrolledFrame(container)
        # vframe.pack(fill=BOTH);
        # vframe.pack()
        # vframe.pack(side=LEFT, anchor=W)
        vframe.grid()
        rbl = RadioList(vframe.interior, bg="white")
        for x in range(100, 150):
            i = x + 1
            t = n2w(i)
            rbl.add(t, i)
        rbl.grid(columnspan=3)

The scrollbar code is taken from here: https://stackoverflow.com/a/16198198/145682

Complete code is available here: https://github.com/deostroll/pytk

Community
  • 1
  • 1
deostroll
  • 11,661
  • 21
  • 90
  • 161
  • You need to call the frame's `.pack_propagate(0)` to stop it resizing to fit the widgets it contains. – PM 2Ring Jan 06 '17 at 07:12
  • Are you asking how to get the scrollbar to stick to the right, or are you asking how to prevent the window from shrinking to fit its contents? – Bryan Oakley Jan 06 '17 at 12:46
  • @BryanOakley isn't it the `container` that is shrinking? – deostroll Jan 06 '17 at 14:13
  • @PM2Ring even if I read the docs, how am I supposed to get that? – deostroll Jan 06 '17 at 14:14
  • @deostroll: yes, the container is shrinking. Your question is very unclear as written. You ask about moving the scrollbar, but the real problem seems to be asking how to make the inner scrolled frame fill the window. The person who closed this as a duplicate apparently thinks you're asking how to prevent the window from shrinking. You should try to clarify what you want. Do you want the window to remain the same size and the whole scrollbable window to expand, or the scrollable window to stay the same but move the scrollbar to the right, or something else? – Bryan Oakley Jan 06 '17 at 14:24

1 Answers1

0

I think the problem is that you aren't configuring your main window properly. If you want the column that contains the scrollable frame to fill the window, you must configure the column to have a "weight" which tells tkinter how to allocate extra space.

For example:

class App(Frame):
    def __init__(self, parent=None):
        ...
        self.grid_columnconfigure(0, weight=1)
        container.grid(row=1, columnspan=3)

Unfortunately, you do not give enough code to know what is actually happening. It could be that the inner frame isn't expanding to fill the container, it could be that the container isn't expanding to fill App, or it could be that App isn't expanding to fill the root window. You might need to configure the column that App is in, and/or configure the column that container is in.

If App is the only thing that is in the root window, you might want to consider using pack instead of grid since you don't have to take this extra step of configuring the weight of rows and columns. Likewise, if vframe is the only thing in container, you might want to use pack for that, too.

The trick is to be methodical and to try to solve only one layout problem at a time. First, make sure that App is filling the window. Then make sure that container is filling whatever part of the window it should fill. And then, make sure that vframe is filling the part of the window it should fill.

Remember that for any container in which you use grid to manage children, you should always call grid_rowconfigure and grid_columnconfigure on at least one row and one column to give a weight.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685