如何在 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 千+ 次浏览

开启你的事业

完成课程以获得认证

上手
广告