How can I put two buttons next to each other in Tkinter?

TkinterServer Side ProgrammingProgramming

Tkinter generally provides three general ways to define the geometry of the widgets. They are − Place, Pack, and Grid Management. If we use the Pack geometry manager, then place the two buttons in the frame using side property. It places the buttons horizontally stacked in the window in (Left, Right, Top and Bottom) direction. The side property maintains the same width and internal padding between all the adjacent widget in the application.


#Import the required Libraries
from tkinter import *
from tkinter import ttk
import random

#Create an instance of Tkinter frame
win = Tk()
#Set the geometry of Tkinter frame

def clear():
def display_num():
   for i in range(1):
      entry.insert(0, random.randint(5,20))

#Define an Entry widget
entry= Entry(win, width= 40)
#Create Buttons with proper position
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= TOP)
button2= ttk.Button(win, text= "Clear", command= clear)



Running the above code will display a window that contains two Buttons horizontally stacked adjacent to each other.

Now, click each button to see the resultant output.

Published on 03-May-2021 09:01:07