Python 模拟tkinter pack几何体中的上、下、左和右边距

Python 模拟tkinter pack几何体中的上、下、左和右边距,python,tkinter,Python,Tkinter,如何使左(青色)和右(品红)边框的垂直高度从上(青色)到下(品红)不等 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

如何使左(青色)和右(品红)边框的垂直高度从上(青色)到下(品红)不等

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()

所以我想我知道你的问题是什么。您正在将上下页边距打包到左右页边距之前。不设置任何特定的边距大小

如果您将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')
希望这能解决你的问题:)