Friday, December 27, 2024
Google search engine
HomeLanguagesHow to add a margin to a tkinter window?

How to add a margin to a tkinter window?

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




# 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()


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




# 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()


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




# 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()


Output :  

 

RELATED ARTICLES

Most Popular

Recent Comments