问题
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.
回答1:
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.
回答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.
回答3:
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()
来源:https://stackoverflow.com/questions/48374484/how-to-have-an-l-shaped-layout-with-pack