我正在嘗試制作一個應用程式,但遇到了問題。有一個Frame:
status_frame = Frame(self.root, height=60, style='status.TFrame') # white one at the top
status_frame.pack_propagate(0)
status_frame.pack(fill=X)
有一個Label:
status_label = Label(status_frame, text='Label', style='status_bold.TLabel')
并用pack()方法放置它:
status_label.pack(side=LEFT, padx=5)
padx=5 作業正常:

但如果我添加pady=25它不動,它會奇怪地削減:

為什么會這樣,我該如何Label向上移動?我需要把它放在pack(). 完整代碼:
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.geometry('400x250')
root.resizable(0, 0)
style = Style()
style.configure('status.TFrame', background='white')
style.configure('status_bold.TLabel', background='white', font=('Arial 9 bold'))
style.configure('status.TLabel', background='while')
status_frame = Frame(root, height=60, style='status.TFrame')
status_frame.pack_propagate(0)
main_frame = Frame(root, height=150)
main_frame.pack_propagate(0)
button_frame = Frame(root, height=40)
button_frame.pack_propagate(0)
status_label = Label(status_frame, text='Label', style='status_bold.TLabel')
left_button = Button(button_frame, text='Left')
right_button = Button(button_frame, text='Right')
status_frame.pack(fill=X)
Separator(root, orient=HORIZONTAL).pack(fill=X)
main_frame.pack(fill=X)
Separator(root, orient=HORIZONTAL).pack(fill=X)
button_frame.pack(fill=X)
status_label.pack(side=LEFT, padx=5, pady=25)
right_button.pack(side=RIGHT, padx=5)
left_button.pack(side=RIGHT)
root.mainloop()
uj5u.com熱心網友回復:
由于您已關閉幾何傳播status_frame并強制空間高度為 60 像素,因此標簽和頂部和底部的 25 像素填充空間不足。使用該填充,它只為標簽留下 10 個像素。Tkinter別無選擇,只能從標簽中洗掉像素。
如果您嘗試使用pady向下移動標簽的文本,您可以像這樣在頂部添加填充:
status_label.pack(side=LEFT, padx=5, pady=(25, 0))
如果你想填充在頂部和底部的兩個,那么你應該不會把幾何傳播關閉。就我個人而言,我認為關閉傳播是正確做法的情況很少見。通過將傳播留在原地,框架將增長或縮小,以便它始終適合子小部件,這在 99 % 的情況下都是您想要的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/356580.html
下一篇:干凈地退出PythonTkinter應用程式,同時還在“after”回圈中的按鈕上使用“wait_variable”函式
