【问题标题】:Simulating top, bottom, left and right margins in tkinter pack geometry在 tkinter 包几何中模拟顶部、底部、左侧和右侧边距
【发布时间】:2016-07-18 08:56:20
【问题描述】:

应该怎么做才能让 left_margin (cyan) 和 right_margin (magenta) 帧全部垂直从 top_marginbottom_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()

【问题讨论】:

    标签: python tkinter


    【解决方案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')
    

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

    【讨论】: