我面临着将侧面堆叠的框架居中放置在父框架中的问题。我知道如何将单个框架居中放置在一个框架中,但是我没有找到一种简单的方法来对其中的几个框架执行此操作。
我得到以下窗口
从下面的代码:
import Tkinter as tk
root = tk.Tk()
root.geometry("200x200")
# main frame
f = tk.Frame(root, background='black')
f.pack(expand=True, fill="both")
# two side-by-side frames inside, they fill up their space
f1 = tk.Frame(f, background='green')
f1.pack(side=tk.LEFT, expand=True, fill="both")
f2 = tk.Frame(f, background='red')
f2.pack(side=tk.LEFT, expand=True, fill="both")
# three fixed-size frames in the left frame above; I would like them to be centered in the frame
tk.Frame(f1, width=20, height=20, background="orange").pack(side=tk.LEFT, fill=None, expand=False)
tk.Frame(f1, width=20, height=20, background="white").pack(side=tk.LEFT, fill=None, expand=False)
tk.Frame(f1, width=20, height=20, background="gray50").pack(side=tk.LEFT, fill=None, expand=False)
root.mainloop()
我希望将三个方形框架以绿色为中心。我必须使用tk.LEFT
它们来定位它们,否则默认情况下它们将被堆叠起来。
在我完整的程序中,绿色框在那里专门包含三个正方形框。
将三个方形框架居中绿色的最标准方法是什么?
在思考furas的评论时,我意识到我不了解expand
和之间的真正区别fill
(仍然有点含糊)。通过将f1.pack()行更改为:可以将三个框架居中:
f1.pack(side=tk.LEFT, expand=True, fill=None)
的f1
帧是紧绕三平方(fill=None
)的人的击打试图采取如在所有方向上(多的空间可能expand=True
),从而有效地为中心。请注意,绿色背景是不可见的,边框紧紧围绕其内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句