+1 (315) 557-6473 

Python Program to Implement Tkinter Assignment Solution.


Write a program to implement tkinter in python.

Requirements and Specifications

program to implement tkinter in python
program to implement tkinter in python 1
program to implement tkinter in python 2
program to implement tkinter in python 3

Source Code


Created on Feb 13, 2022


@note: This module handles the creation and functionality of the ImageViewer component, which enables

the user to view an image


from tkinter import *

from PIL import ImageTk, Image


NOTE: For some reason, the PhotoImage from Tkinter won't load images and I kept getting

an error saying that the data from the image could not be load. I googled a solution for

this problem and found that using the ImageTk and Image from PIL would fix the problem

I found that solution here: https://stackoverflow.com/questions/47357090/tkinter-error-couldnt-recognize-data-in-image-file


# Create a class for imageviewer

class ImageViewer:

def __init__(self):



:param window: Tk WIndow

:param frame: Frame to put buttons


self.canvas = None

self.view_btn = None

self.close_btn = None

self.window = None

self.buttons_frame = None

self.img = None

def initialize(self, window, frame):


Initialize all components for ImageViewer



self.window = window

self.buttons_frame = frame

# Create a Label that will work as canvas

self.canvas = Label(self.window, text="")


# Create buttons

# Create its two buttons

self.view_btn = Button(self.buttons_frame, text="View", command=lambda: self.view())

self.close_btn = Button(self.buttons_frame, text="Close", command=lambda: self.close())

# Add the buttons to a list named buttons

self.buttons = [self.view_btn, self.close_btn]

# Add other components (canvas, text editor, etc) to another list

self.objects = [self.canvas]




def show(self, components):


This function will show all required components in the UI

Also, this function receives a 'components' list that contains all other components for

all other editors (image viewer, video player, etc)

The function will take the other components and hide its widgets

:param components: List of components



# Hide buttons, canvas etc for all other components

for component in components:


self.canvas.pack(expand=True, fill="both")

self.view_btn.grid(column=1, row=0)

self.close_btn.grid(column=2, row=0)

def view(self):


This function loads the image and displays it



self.img = ImageTk.PhotoImage(Image.open('../raw/image.jpg'))


self.canvas.photo_ref = self.img

def close(self):


This function hides all widgets/components for the ImageViewer module



self.canvas.config(image = None)

self.canvas.photo_ref = None