+1 (315) 557-6473 

Python Program to Implement Tkinter Assignment Solution.


Instructions

Objective
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

@author:

@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):

"""

Constructor

: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

:return:

"""

self.window = window

self.buttons_frame = frame

# Create a Label that will work as canvas

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

self.canvas.pack(side=TOP)

# 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]

self.canvas.pack_forget()

self.view_btn.pack_forget()

self.close_btn.pack_forget()

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

:return:

"""

# Hide buttons, canvas etc for all other components

for component in components:

component.close()

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

:return:

"""

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

self.canvas.config(image=self.img)

self.canvas.photo_ref = self.img

def close(self):

"""

This function hides all widgets/components for the ImageViewer module

:return:

"""

self.canvas.config(image = None)

self.canvas.photo_ref = None

self.canvas.pack_forget()

self.view_btn.grid_remove()

self.close_btn.grid_remove()