如何正确对齐 "side=BOTTOM"/"anchor=S" 的项目?

How to align items with "side=BOTTOM"/"anchor=S" properly?

我有一个 GUI 应用程序,其中底部有一个底部滚动条和一个底部状态栏。我想以滚动条位于底部状态栏上方的方式对齐它们(在官方记事本中):

Here statusbar is at bottom and scrollbar is at top.

但是我做的是:

Here statusbar is at top and scrollbar is at bottom.

我也试过 anchor=S 但也没有用并抛出相同的结果。

代码:(状态栏)

    statusbar = Frame(textarea)
    statusbar.pack(side=BOTTOM, fill=X)
    statusbar_Content1 = Label(statusbar, anchor=SW, width=18, text="Ln 1, col 1")
    statusbar_Content2 = Label(statusbar, anchor=SW, text="100%")
    statusbar_Content3 = Label(statusbar, anchor=SW, width=15, text="Windows (CRLF)")
    statusbar_Content4 = Label(statusbar, anchor=SW, width=13, text="UTF-8")
    statusbar_Content4.pack(side=RIGHT)
    Label(statusbar, anchor=SW, text="|", state=DISABLED).pack(side=RIGHT)
    statusbar_Content3.pack(side=RIGHT)
    Label(statusbar, anchor=SW, text="|", state=DISABLED).pack(side=RIGHT)
    statusbar_Content2.pack(side=RIGHT)
    Label(statusbar, anchor=SW, text="|", state=DISABLED).pack(side=RIGHT)
    statusbar_Content1.pack(side=RIGHT)
    Label(statusbar, anchor=SW, text="|", state=DISABLED).pack(side=RIGHT)

代码:(滚动条)

Scroll2 = Scrollbar(root, orient=HORIZONTAL)
Scroll2.pack(side=BOTTOM, fill=X)
Scroll2.config(command=textarea.xview)

CODE : (Scrollbar is bound with Text widget)

textarea = Text(root, font=fontsmain, yscrollcommand=Scroll.set,xscrollcommand=Scroll2.set, undo=True)
textarea.pack(expand=True, fill=BOTH)

结论:

Is there any way of setting these such that SCROLLBAR come above STATUSBAR and vise versa ?

pack 通过在未分配的 space 的一侧放置一个小部件来工作。这意味着结果取决于小部件的打包顺序。这称为 堆叠顺序

例如,如果您先打包滚动条,它将被放置在 window 的最底部,因为还没有 space 分配给任何东西。当您稍后在状态栏上调用 pack 时,window 的最底部已经分配,​​因此它将被放置在滚动条上方空闲 space 的底部。

知道这一点,当您更改调用 pack 的顺序时,您会更改小部件在 window 中出现的顺序。因此,一个简单的解决方案是确保在打包滚动条之前打包状态栏。

pack 也有一些选项可以让您指定一个小部件是在排序中的某个其他小部件之前还是之后。例如,您可以打包滚动条,然后打包状态栏,但告诉它在堆叠顺序中出现在滚动条之前。当您使用 side='bottom' 时,状态栏将出现在滚动条下方。

注意以下三个示例之间的区别:

statusbar.pack(side="bottom", fill="x")
scrollbar.pack(side="bottom", fill="x")

scrollbar.pack(side="bottom", fill="x")
statusbar.pack(side="bottom", fill="x")

scrollbar.pack(side="bottom", fill="x")
statusbar.pack(side="bottom", fill="x", before=scrollbar)