Layout Managers

layout managers
There are 3 layout managers in Tkinter – pack(), grid() and place().
Layout managers can’t be mixed inside a widget – 1 widget can only have 1 layout manager.

This code snippet uses all 3 layout managers. Main Frame includes 3 other frames.
Inside each of these 3 frames, one of the layout managers is presented.

source 1 – python course
source 2 – tutorials point
source 3 – effbot

from tkinter import *

class Application(Frame):
    
    def __init__(self, master):
        super().__init__(master)
        # not resizable in the x, y directions
        master.resizable(0,0)
        self.grid()
        self.createWidgets()
              
    def createWidgets(self):
        #frames (left, right and bottom are placed with grid layout manager)
        #-------------------------------------------------------------------
        # LEFT FRAME - GRID() inside
        self.FrameLeft = Frame(self, bg = "grey")
        self.FrameLeft.grid(row = 0, column = 0)
        
        self.bttnL1 = Button(self.FrameLeft, text = "row = 0, column = 0")
        self.bttnL1.grid(row = 0, column = 0, padx = 10 , pady = 5)
        
        self.bttnL2 = Button(self.FrameLeft, text = "row = 0 , column = 1")
        self.bttnL2.grid(row = 0, column = 1, padx = 10 , pady = 5)
        
        self.bttnL3 = Button(self.FrameLeft,
                              text = "row = 1, column = 0, rowspan = 2")
        self.bttnL3.grid(row = 1, column = 0, columnspan = 2, pady = 5)
        
        self.bttnL4 = Button(self.FrameLeft, text = "grid() layout",
                             bg = "red")
        self.bttnL4.grid(row = 2, column = 0, pady = 5)
        #-------------------------------------------------------------------
        #RIGHT FRAME - PACK() inside
        self.FrameRight = Frame(self,
                                 relief=RAISED, borderwidth=10)
        self.FrameRight.grid(row = 0, column = 1)
        
        self.bttn1R = Button(self.FrameRight, text = "left")
        self.bttn1R.pack(side = LEFT, padx = 10)
        
        self.bttn2R = Button(self.FrameRight, text = "right")
        self.bttn2R.pack(side = RIGHT, padx = 10)
        
        self.bttn3R = Button(self.FrameRight, text = "top")
        self.bttn3R.pack(side = TOP, pady = 10, fill = X)
        
        self.bttn4R = Button(self.FrameRight, text = "bottom")
        self.bttn4R.pack(side = BOTTOM, pady = 10, fill = X)
        
        self.bttn5R = Button(self.FrameRight, text = "pack() layout",
                             bg = "red")
        self.bttn5R.pack()
        #-------------------------------------------------------------------
        #BOTTOM FRAME - PLACE() inside
        self.FrameBottom = Frame(self, bg = "navy", width=550, height=200)   
        self.FrameBottom.grid(row = 1, column = 0, columnspan = 2)
        
        self. bttn1B = Button(self.FrameBottom,
                     text = ".place(relx=.5, rely=.5,width= 250, height=20)")
        self.bttn1B.place(relx=.5, rely=.5, width = 250, height = 20)
        
        self. bttn2B = Button(self.FrameBottom, bg = "red", 
                              text = ".place(x = 10, y = 30)")
        self.bttn2B.place(x = 10, y = 30)

root = Tk()
root.title("Layout Managers")
app = Application(root)
root.mainloop()
About

To contact me write an mail on stodoo@gmail.com or message me through linkedin: www.linkedin.com/in/stodolkiewicz/

Tagged with: , , , ,
Posted in tkinter

Leave a comment

Categories