模拟tkinter包几何中的顶部,底部,左侧和右侧边距

时间:2016-07-18 00:40:29

标签: python tkinter

如何使用 left_margin 青色)和 right_margin magenta )框架进行所有垂直拍摄从 top_margin bottom_margin 的高度?

import Tkinter as tk

root = tk.Tk()

top_margin = tk.Frame(root, height=32, background='red')
left_margin = tk.Frame(root, background='cyan')
sheet_area = tk.Frame(root, background='white')
right_margin = tk.Frame(root, background='magenta')
bottom_margin = tk.Frame(root, height=32, background='blue')

top_margin.pack(side=tk.TOP, expand=tk.YES, fill=tk.X, anchor=tk.N)
bottom_margin.pack(side=tk.BOTTOM, expand=tk.YES, fill=tk.X, anchor=tk.S)
left_margin.pack(side=tk.LEFT, expand=tk.YES, fill=tk.BOTH)
right_margin.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.BOTH)

root.mainloop()

1 个答案:

答案 0 :(得分:1)

所以我想我知道你的问题是什么。您在左右之前打包顶部和底部边距。没有为边距设置任何特定大小。

如果您将pack语句更改为如下所示:

left_margin.pack(side=tk.LEFT, expand=tk.YES, fill=tk.BOTH)
right_margin.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.BOTH)
top_margin.pack(side=tk.TOP, expand=tk.YES, fill=tk.X, anchor=tk.N)
bottom_margin.pack(side=tk.BOTTOM, expand=tk.YES, fill=tk.X, anchor=tk.S)

如果您希望顶部和底部边距可见,请添加宽度。像这样:

bottom_margin = tk.Frame(root, width = 10, height=32, background='blue')
top_margin = tk.Frame(root, width = 10, height=32, background='red')

希望这能解决你的问题:)