如何使用包进行 L 形布局?
How to have an L shaped layout with pack?
我有:
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()
其布局使用 grid
。我怎样才能只使用 pack
而使用 而不使用 使用其他小部件来获得相同的 L 形布局?
Frames
' 父级(不一定是顶级)将只有这 3 个 Frames
作为子级。理想情况下,布局应该可以调整大小。
它不能完成。下面的演示显示了每个 side
选项的所有可能形状:
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()
frames['red'].pack(anchor=tk.W)
frames['green'].pack(side=tk.LEFT)
frames['blue'].pack()
但是你为什么要这样做?将 Frames 用于整齐排列的行或列,然后将它们打包成更复杂的结构要容易得多。
在这种特定情况下,如果您希望每个帧占据屏幕的 1/4 并在 window 调整大小时展开,pack
根本无法做到。当您调整 window 大小时,pack
无法使左上角的小部件占据屏幕的 1/4,也没有额外的帧。
Tkinter 有 pack
和 grid
因为它们解决了两种不同类型的布局问题。如果他们能轻松解决所有相同的问题,就没有必要同时拥有他们了。
我有:
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()
其布局使用 grid
。我怎样才能只使用 pack
而使用 而不使用 使用其他小部件来获得相同的 L 形布局?
Frames
' 父级(不一定是顶级)将只有这 3 个 Frames
作为子级。理想情况下,布局应该可以调整大小。
它不能完成。下面的演示显示了每个 side
选项的所有可能形状:
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()
frames['red'].pack(anchor=tk.W)
frames['green'].pack(side=tk.LEFT)
frames['blue'].pack()
但是你为什么要这样做?将 Frames 用于整齐排列的行或列,然后将它们打包成更复杂的结构要容易得多。
在这种特定情况下,如果您希望每个帧占据屏幕的 1/4 并在 window 调整大小时展开,pack
根本无法做到。当您调整 window 大小时,pack
无法使左上角的小部件占据屏幕的 1/4,也没有额外的帧。
Tkinter 有 pack
和 grid
因为它们解决了两种不同类型的布局问题。如果他们能轻松解决所有相同的问题,就没有必要同时拥有他们了。