如何在 Tkinter 視窗中新增邊距?


可以透過指定 fillexpandpadding 的值來控制 Tkinter 視窗的邊距。設定 Tkinter 視窗邊距的另一種方法是使用 grid(**options) 幾何管理器。網格包管理器允許我們透過指定行和列屬性的值來新增邊距。

示例

# Import the required library
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win= Tk()

# Set the size of the Tkinter window
win.geometry("700x350")

# Add a frame to set the size of the window
frame= Frame(win, relief= 'sunken', bg= "black")
frame.pack(fill= BOTH, expand= True, padx= 10, pady=20)

# Add a label widget
label= Label(frame, text= "Welcome to Tutorialspoint",
font=('Helvetica 15 bold'), bg= "white")
label.pack(pady= 30)

win.mainloop()

輸出

執行以上程式碼將顯示一個 Label Text,位於一個框架內。框架的邊距是可擴充套件且可調整大小的。

更新於: 07-6 月-2021

7 千+ 次瀏覽

開啟你的事業

完成課程以獲得認證

上手
廣告
© . All rights reserved.