How to add a margin to a tkinter window?
Last Updated :
18 Nov, 2021
In this article, we will see how to add a margin to the Tkinter window.
We will use the frame for adding the margin:
Syntax:
Frame(root, options)
Approach:
- Importing the module.
- Create the main window (container)
- Use frame and frame.pack()
- Apply the event Trigger on the widgets.
Without using frame method:
Python3
from tkinter import *
root = Tk()
label = Label(root, text = 'GeeksForGeeks.org!' ,
width = 45 , height = 10 )
label.pack()
root.mainloop()
|
Output :
Example 1:
By using the frame method of this module. With frame() we have used pack() function to place the content and to create margin.
window.pack(options)
Options are fill, expand or side.
Below is the implementation:
Python3
from tkinter import *
root = Tk()
frame = Frame(root, relief = 'sunken' ,
bd = 1 , bg = 'white' )
frame.pack(fill = 'both' , expand = True ,
padx = 10 , pady = 10 )
label = Label(frame, text = 'GeeksForGeeks.org!' ,
width = 45 , height = 10 , bg = "black" ,
fg = "white" )
label.pack()
root.mainloop()
|
Output :
Example 2: We can also the grid() method of the module to give margin to the container or window.
Syntax :
window.grid(grid_options)
Python3
from tkinter import *
root = Tk()
frame = Frame(root)
frame.text = Text(root)
frame.text.insert( '1.0' , 'Geeks for Geeks' )
frame.text.grid(row = 0 , column = 1 ,
padx = 20 , pady = 20 )
frame.quitw = Button(root)
frame.quitw[ "text" ] = "Logout" ,
frame.quitw[ "command" ] = root.quit
frame.quitw.grid(row = 1 , column = 1 )
root.mainloop()
|
Output :
Share your thoughts in the comments
Please Login to comment...