如何向 tkinter 窗口添加边距?
在本文中,我们将看到如何向 Tkinter 窗口添加边距。
我们将使用框架来添加边距:
句法:
Frame(root, options)
方法:
- 导入模块。
- 创建主窗口(容器)
- 使用 frame 和 frame.pack()
- 在小部件上应用事件触发器。
不使用框架方法:
Python3
# importing the module
from tkinter import *
# main container
root = Tk()
# container content
label = Label(root, text='GeeksForGeeks.org!',
width=45, height=10)
label.pack()
root.mainloop()
Python3
# importing module
from tkinter import *
# main container
root = Tk()
# frame
frame = Frame(root, relief = 'sunken',
bd = 1, bg = 'white')
frame.pack(fill = 'both', expand = True,
padx = 10, pady = 10)
# container content
label = Label(frame, text = 'GeeksForGeeks.org!',
width = 45, height = 10, bg = "black",
fg = "white")
label.pack()
root.mainloop()
Python3
# importing the module
from tkinter import *
# container window
root = Tk()
# frame
frame = Frame(root)
# content of the frame
frame.text = Text(root)
frame.text.insert('1.0', 'Geeks for Geeks')
# to add margin to the frame
frame.text.grid(row = 0, column = 1,
padx = 20, pady = 20)
# simple button
frame.quitw = Button(root)
frame.quitw["text"] = "Logout",
frame.quitw["command"] = root.quit
frame.quitw.grid(row = 1, column = 1)
root.mainloop()
输出 :
示例 1:
通过使用该模块的框架方法。在 frame() 中,我们使用了 pack()函数来放置内容并创建边距。
window.pack(options)
Options are fill, expend or side.
下面是实现:
蟒蛇3
# importing module
from tkinter import *
# main container
root = Tk()
# frame
frame = Frame(root, relief = 'sunken',
bd = 1, bg = 'white')
frame.pack(fill = 'both', expand = True,
padx = 10, pady = 10)
# container content
label = Label(frame, text = 'GeeksForGeeks.org!',
width = 45, height = 10, bg = "black",
fg = "white")
label.pack()
root.mainloop()
输出 :
示例 2:我们也可以通过模块的 grid() 方法为容器或窗口提供边距。
句法 :
window.grid(grid_options)
蟒蛇3
# importing the module
from tkinter import *
# container window
root = Tk()
# frame
frame = Frame(root)
# content of the frame
frame.text = Text(root)
frame.text.insert('1.0', 'Geeks for Geeks')
# to add margin to the frame
frame.text.grid(row = 0, column = 1,
padx = 20, pady = 20)
# simple button
frame.quitw = Button(root)
frame.quitw["text"] = "Logout",
frame.quitw["command"] = root.quit
frame.quitw.grid(row = 1, column = 1)
root.mainloop()
输出 :