Organizing Layout And Widgets :

import tkinter

# Let's create the Tkinter window.
window = tkinter.Tk()
window.title("GUI")

# Create a division with the help of Frame class and align them on TOP and BOTTOM with pack()
top_frame = tkinter.Frame(window).pack()
bottom_frame = tkinter.Frame(window).pack(side="bottom")

# Add widgets in both the frames.
btn1 = tkinter.Button(top_frame, text="Button1", fg="red").pack()
btn2 = tkinter.Button(top_frame, text="Button2", fg="green").pack()

btn3 = tkinter.Button(bottom_frame, text="Button3", fg="purple").pack(side="left")
btn4 = tkinter.Button(bottom_frame, text="Button4", fg="orange").pack(side="left")

window.mainloop()

Output :

widegets


Grid :

import tkinter
from tkinter import *

top = tkinter.Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()

tkinter.Checkbutton(
    top,
    text = "Machine Learning",
    variable = CheckVar1,
    onvalue = 1,
    offvalue = 0
).grid(row=0, sticky=W)

tkinter.Checkbutton(
    top,
    text = "Deep Learning",
    variable = CheckVar2,
    onvalue = 0,
    offvalue = 1
).grid(row=1, sticky=W)

top.mainloop()

Output :

output


Another example Grid :

import tkinter
# Let's create the Tkinter window
window = tkinter.Tk()
window.title("GUI")

# You will create two text labels: 'username' and 'password'
tkinter.Label(window, text="Username").grid(row=0) 
# 'Entry' for username input
tkinter.Entry(window).grid(row=0, column=1) 

tkinter.Label(window, text="Password").grid(row=1) 
tkinter.Entry(window).grid(row=1, column=1) 

# Checkbutton across two columns
tkinter.Checkbutton(window, text="Keep Me Logged In").grid(columnspan=2)

window.mainloop()

Output :

output


Binding or Command Functions :

import tkinter
# Let's create the Tkinter window
window = tkinter.Tk()
window.title("GUI")

# creating a function called DataCamp_Tutorial()
def DataCamp_Tutorial():
    tkinter.Label(window, text="GUI with Tkinter!").pack()

tkinter.Button(window, text="Click Me!", command=DataCamp_Tutorial).pack()

window.mainloop()

Output :

output


Mouse Clicking Event via the Bind Method :

import tkinter
# Let's create the Tkinter window
window = tkinter.Tk()
window.title("GUI")

# You will create three different functions for three different events
def left_click(event):
    tkinter.Label(window, text="Left Click!").pack()

def middle_click(event):
    tkinter.Label(window, text="Middle Click!").pack()

def right_click(event):
    tkinter.Label(window, text="Right Click!").pack()

window.bind("<Button-1>", left_click)
window.bind("<Button-2>", middle_click)
window.bind("<Button-3>", right_click)

window.mainloop()

Output :

output


Alert Boxes :

import tkinter
import tkinter.messagebox

# Let's create the Tkinter window
window = tkinter.Tk()
window.title("GUI")

# Create an alert box using messagebox
tkinter.messagebox.showinfo("Alert Message", "This is just a alert message!")

# Ask a Yes/No question
response = tkinter.messagebox.askquestion("Tricky Question", "Do you love Deep Learning?")

# Display response
if response == "yes":
    tkinter.Label(window, text="Yes, offcourse I love Deep Learning!").pack()
else:
    tkinter.Label(window, text="No, I don't love Deep Learning!").pack()

window.mainloop()

Output :

output

output


Rendering Images :

import tkinter

# Let's create the Tkinter window
window = tkinter.Tk()
window.title("GUI")

# Load an image using PhotoImage and store it in a variable
icon = tkinter.PhotoImage(file="CNN.png")

# Display the image inside a label
label = tkinter.Label(window, image=icon)
label.pack()

window.mainloop()

Output :

output


Creating a Calculator with Tkinter :


from tkinter import *

# Let's create the Tkinter window
window = Tk()
window.geometry("312x324")
window.resizable(0, 0)
window.title("Calculator")

# Functions
def btn_click(item):
    global expression
    expression += str(item)
    input_text.set(expression)

def btn_clear():
    global expression
    expression = ""
    input_text.set("")

def btn_equal():
    global expression
    result = str(eval(expression))
    input_text.set(result)
    expression = ""

expression = ""
input_text = StringVar()

# UI Layout
input_frame = Frame(window, width=312, height=50, bd=0, highlightbackground="black", highlightthickness=1)
input_frame.pack(side=TOP)

input_field = Entry(input_frame, font=('arial', 18, 'bold'), textvariable=input_text, width=50, bg="#eee", bd=0, justify=RIGHT)
input_field.grid(row=0, column=0)
input_field.pack(ipady=10)

btns_frame = Frame(window, width=312, height=272.5, bg="grey")
btns_frame.pack()

# Button Rows
Button(btns_frame, text="C", width=32, height=3, command=btn_clear).grid(row=0, column=0, columnspan=3)
Button(btns_frame, text="/", width=10, height=3, command=lambda: btn_click("/")).grid(row=0, column=3)

# You can continue with 7-9, 4-6, 1-3, 0 .=+ the same way...

window.mainloop()

Output :

output