python

How to Make a Clickable Images using Python

This tutorial will teach you how to make a Clickable Images using Python.

Step 1 Code

from tkinter import *
from tkinter import messagebox

from PIL import Image, ImageTk

root = Tk()
canvas = Canvas(root, width=600, height=600)
canvas.pack()


def first():
    messagebox.showinfo('First','You clicked the first image')

def second():
    messagebox.showinfo('First','You clicked the second image')

def third():
    messagebox.showinfo('First','You clicked the third image')

img_file = Image.open("1.jpg")
img_file = img_file.resize((150, 150))
img = ImageTk.PhotoImage(img_file)
b1 = Button(canvas,image=img,command=first).pack()


img_file1 = Image.open("2.jpg")
img_file1 = img_file1.resize((150, 150))
img1 = ImageTk.PhotoImage(img_file1)
b2 = Button(canvas,image=img1,command=second).pack()

img_file2 = Image.open("3.jpg")
img_file2 = img_file2.resize((150, 150))
img2 = ImageTk.PhotoImage(img_file2)
b3 = Button(canvas,image=img2,command=third).pack()

root.mainloop()

Step 2 Code

from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk

root = Tk()
canvas = Canvas(root, width=600, height=600)
canvas.pack()

def main(event):
    if event.widget.cget('image') == 'pyimage1':
        messagebox.showinfo('First','You clicked the first image')
    elif event.widget.cget('image') == 'pyimage2':
        messagebox.showinfo('Second','You clicked the second image')
    elif event.widget.cget('image') == 'pyimage3':
        messagebox.showinfo('Third','You clicked the third image')

img_file = Image.open("1.jpg")
img_file = img_file.resize((150, 150))
img = ImageTk.PhotoImage(img_file)
b1 = Button(canvas, image=img)
b1.pack()
b1.bind('<Button-1>', main)


img_file1 = Image.open("2.jpg")
img_file1 = img_file1.resize((150, 150))
img1 = ImageTk.PhotoImage(img_file1)
b2 = Button(canvas, image=img1)
b2.pack()
b2.bind('<Button-1>', main)

img_file2 = Image.open("3.jpg")
img_file2 = img_file2.resize((150, 150))
img2 = ImageTk.PhotoImage(img_file2)
b3 = Button(canvas, image=img2)
b3.pack()
b3.bind('<Button-1>', main)

root.mainloop()

 

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

1 week ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

2 weeks ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

2 weeks ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

2 weeks ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

4 weeks ago

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…

1 month ago