0

I have:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


if __name__ == '__main__':
    root = tk.Tk()
    rgb = {'red', 'green', 'blue'}
    frames = dict()
    for color in rgb:
        frames[color] = tk.Frame(root, height=64, width=64, bg=color)
    frames['red'].grid(row=0, column=0)
    frames['green'].grid(row=1, column=0)
    frames['blue'].grid(row=1, column=1)
    root.mainloop()

which uses grid for its layout. How can I have the same, L shaped, layout using only pack instead and without using additional widgets?

Frames' parent(not necessarily a top-level) will have only those 3 Frames as children. Ideally the layout should be resizeable.

Nae
  • 14,209
  • 7
  • 52
  • 79

3 Answers3

2

In this specific case, if you want each frame to take up 1/4th of the screen and expand when the window resizes, pack is simply not able to do it. There is no way with pack and no extra frames to get the upper-left widget to take up 1/4th of the screen when you resize the window.

Tkinter has both pack and grid because they solve two different types of layout problems. If they could easily solve all of the same problems there would be no need to have them both.

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

It can't be done. Below demo displays all shapes possible for every side option:

import tkinter as tk


class ParentFrame(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self._colors = ('red', 'green', 'blue')
        self._c_len = len(self._colors)
        self.labels = dict()
        self._i = 0
        self._create_widgets()
        self.side_combinations()


    def _create_widgets(self):
        for _color in self._colors:
            self.labels[_color] = tk.Label(self, height=16, width=16,
                                                fg='white', bg=_color)


    def side_combinations(self):
        _side_opts = ('top', 'bottom', 'left', 'right')
        _o_len = len(_side_opts)
        if self._i < (_o_len**3):
            _cur_opt = dict()
            _cur_opt['red'] = _side_opts[self._i//(_o_len**2)]
            _cur_opt['green'] = _side_opts[(self._i//_o_len) % _o_len]
            _cur_opt['blue'] = _side_opts[self._i%_o_len]

            for _clr in self._colors:
                self.labels[_clr].pack(side=_cur_opt[_clr])
                self.labels[_clr]['text'] = _cur_opt[_clr]

            self._i += 1
            self.after(250, self.side_combinations)
        else:
            self._i = 0


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

    test = ParentFrame(root)
    test.pack(fill='both', expand=True)

    root.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79
0
frames['red'].pack(anchor=tk.W)
frames['green'].pack(side=tk.LEFT)
frames['blue'].pack()

But why would you want to? It's much easier to use Frames for the things that fall into neat rows or columns, and then pack the those into more complex structures.

Novel
  • 13,406
  • 2
  • 25
  • 41
  • I tried to solve a similar question, but couldn't. Wondered if it was at all possible, apparently, it is. Besides, I think this may be a good dupe-loc for stubborn questions demanding `pack`. Thanks. – Nae Jan 22 '18 at 06:58
  • Any scalable way, btw? – Nae Jan 22 '18 at 07:00
  • 1
    As soon as you resize the window you'll see this isn't the same at all. – Bryan Oakley Jan 22 '18 at 14:14