Python Tkinter - Frame

Pybeginner
By -
0



A Frame is a rectangular region on the screen. A Frame can also be used as a base class to implement complex widgets. It is used to organize a group of widgets.

And frames can be very useful for better organizing the structure of your application, in a friendlier and easier to navigate way.

 

Syntax:

The syntax for using the check button is provided below.

f = Frame(window, options)

 

The complete code will be like :

 

from tkinter import *

window = Tk()
window.title("Frame")
window.geometry('300x200')

f_1 = Frame(window, width = 100, height = 100, bg = "blue")
f_1.grid(row = 0, column = 0)

window.mainloop()

 



We can make use of frames, as it facilitates the organization of items within the window.

And to create more than one frame we will simply do this:

 

from tkinter import *

window = Tk()
window.title("Frame")
window.geometry('320x250')


f_1 = Frame(window, width = 150, height = 100 , bg = "blue")
f_1.grid(row = 0, column = 0, padx = 5, pady = 5)

f_2 = Frame(window, width = 150, height = 100, bg = "yellow")
f_2.grid(row = 0, column = 1, padx = 5, pady = 5)

f_3 = Frame(window, width = 150, height = 100, bg = "red")
f_3.grid(row = 1, column = 0, padx = 5, pady = 5)

f_4 = Frame(window, width = 150, height = 100, bg = "green")
f_4.grid(row = 1, column = 1, padx = 5, pady = 5)

window.mainloop()

 




Python Tkinter - how to place items inside a frame

 

To place items in a frame, instead of using the main window, let's simply call the name of the frame in which we want the item to be placed:

 

from tkinter import *

window = Tk( )
window.title("Frame")
window.geometry('320x250')


f_1 = Frame(window, width = 150, height = 100, bg = "blue")
f_1.grid(row = 0, column = 0, padx = 5, pady = 5)

f_2 = Frame(window, width = 150, height = 100, bg = "yellow")
f_2.grid(row = 0, column = 1, padx = 5, pady = 5)

f_3 = Frame(window, width = 150, height = 100, bg = "red")
f_3.grid(row = 1, column = 0, padx = 5, pady = 5)

f_4 = Frame(window, width = 150, height = 100, bg = "green")
f_4.grid(row = 1, column = 1, padx = 5, pady = 5)

# placing items in the first frame
l_1 = Label(f_1, text = "Blue frame", width = 22)
l_1.grid(column = 0, row = 0, stick = NSEW)

l_2 = Label(f_2, text = "Yellow frame", width = 22)
l_2. grid(column = 0, row = 0)

l_3 = Label(f_3, text = "Red frame", width = 22)
l_3.grid(column = 0, row = 0, stick = NSEW)

l_4 = Label(f_4, text = "Green frame", width = 22)
l_4.grid(column = 0, row = 0)


window.mainloop()

 



Using the grid geometry manager, you will notice that the background colors of the frames have disappeared, this is because the manager grid assigns the height and width properties of the frames to the items placed within it, but to other geometry managers, such as place and pack, this behavior does not exist.

Post a Comment

0Comments

Post a Comment (0)