Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to create a COVID19 Data Representation GUI?
Next article icon

Prerequisites:Python GUI – tkinter,Python: Pillow

Have you ever wondered to make a Image viewer with the help of Python? Here is a solution to making the Image viewer with the help of Python. We can do this with the help of Tkinter and pillow. We will discuss the module needed and code below. 

Module Needed

  • Tkinter:Tkinter is library with the help of which we can make GUI(Graphical  User Interface).
 pip install tkinter
  • Pillow:We can add photos as it is an imaging library of Python.
 pip install pillow

Now let's code for it

Getting Started

  • Below Code demonstrates the basic structures, button initialization, and layout of the GUI produced
Python
# importing the tkinter module and PIL that# is pillow modulefromtkinterimport*fromPILimportImageTk,Image# Calling the Tk (The initial constructor of tkinter)root=Tk()# We will make the title of our app as Image Viewerroot.title("Image Viewer")# The geometry of the box which will be displayed# on the screenroot.geometry("700x700")# Adding the images using the pillow module which# has a class ImageTk We can directly add the# photos in the tkinter folder or we have to# give a proper path for the imagesimage_no_1=ImageTk.PhotoImage(Image.open("Sample.png"))image_no_2=ImageTk.PhotoImage(Image.open("sample.png"))image_no_3=ImageTk.PhotoImage(Image.open("Sample.png"))image_no_4=ImageTk.PhotoImage(Image.open("sample.png"))# List of the images so that we traverse the listList_images=[image_no_1,image_no_2,image_no_3,image_no_4]label=Label(image=image_no_1)# We have to show the box so this below line is neededlabel.grid(row=1,column=0,columnspan=3)# We will have three button back ,forward and exitbutton_back=Button(root,text="Back",command=back,state=DISABLED)# root.quit for closing the appbutton_exit=Button(root,text="Exit",command=root.quit)button_forward=Button(root,text="Forward",command=lambda:forward(1))# grid function is for placing the buttons in the framebutton_back.grid(row=5,column=0)button_exit.grid(row=5,column=1)button_forward.grid(row=5,column=2)root.mainloop()
  • Forward function:This function is for adding the functionality to forward button
Python
defforward(img_no):# GLobal variable so that we can have# access and change the variable# whenever neededgloballabelglobalbutton_forwardglobalbutton_backglobalbutton_exitlabel.grid_forget()# This is for clearing the screen so that# our next image can pop uplabel=Label(image=List_images[img_no-1])# as the list starts from 0 so we are# subtracting onelabel.grid(row=1,column=0,columnspan=3)button_for=Button(root,text="forward",command=lambda:forward(img_no+1))# img_no+1 as we want the next image to pop upifimg_no==4:button_forward=Button(root,text="Forward",state=DISABLED)# img_no-1 as we want previous image when we click# back buttonbutton_back=Button(root,text="Back",command=lambda:back(img_no-1))# Placing the button in new gridbutton_back.grid(row=5,column=0)button_exit.grid(row=5,column=1)button_for.grid(row=5,column=2)
  • Backward Function:This function is for adding the functionality to backward button
Python
# importing the tkinter module and PIL# that is pillow modulefromtkinterimport*fromPILimportImageTk,Imagedefforward(img_no):globallabelglobalbutton_forwardglobalbutton_backglobalbutton_exitlabel.grid_forget()label=Label(image=List_images[img_no-1])label.grid(row=1,column=0,columnspan=3)button_forward=Button(root,text="forward",command=lambda:forward(img_no+1))ifimg_no==4:button_forward=Button(root,text="Forward",state=DISABLED)button_back=Button(root,text="Back",command=lambda:back(img_no-1))button_back.grid(row=5,column=0)button_exit.grid(row=5,column=1)button_forward.grid(row=5,column=2)defback(img_no):globallabelglobalbutton_forwardglobalbutton_backglobalbutton_exitlabel.grid_forget()label=Label(image=List_images[img_no-1])label.grid(row=1,column=0,columnspan=3)button_forward=Button(root,text="forward",command=lambda:forward(img_no+1))button_back=Button(root,text="Back",command=lambda:back(img_no-1))ifimg_no==1:button_back=Button(root,text="Back",state=DISABLED)label.grid(row=1,column=0,columnspan=3)button_back.grid(row=5,column=0)button_exit.grid(row=5,column=1)button_forward.grid(row=5,column=2)root=Tk()root.title("Image Viewer")root.geometry("700x700")# Change the png file name a/c to your imageimage_no_1=ImageTk.PhotoImage(Image.open("Sample.png"))image_no_2=ImageTk.PhotoImage(Image.open("Capture3.png"))image_no_3=ImageTk.PhotoImage(Image.open("Sample2.png"))image_no_4=ImageTk.PhotoImage(Image.open("Sample4.png"))List_images=[image_no_1,image_no_2,image_no_3,image_no_4]label=Label(image=image_no_1)label.grid(row=1,column=0,columnspan=3)button_back=Button(root,text="Back",command=back,state=DISABLED)button_exit=Button(root,text="Exit",command=root.quit)button_forward=Button(root,text="Forward",command=lambda:forward(1))button_back.grid(row=5,column=0)button_exit.grid(row=5,column=1)button_forward.grid(row=5,column=2)root.mainloop()

Complete Code

Images Used and their order - 

Order in which the images will be shown.
Python
# importing the tkinter module and PIL# that is pillow modulefromtkinterimport*fromPILimportImageTk,Imagedefforward(img_no):globallabelglobalbutton_forwardglobalbutton_backglobalbutton_exitlabel.grid_forget()label=Label(image=List_images[img_no-1])label.grid(row=1,column=0,columnspan=3)button_forward=Button(root,text="forward",command=lambda:forward(img_no+1))ifimg_no==4:button_forward=Button(root,text="Forward",state=DISABLED)button_back=Button(root,text="Back",command=lambda:back(img_no-1))button_back.grid(row=5,column=0)button_exit.grid(row=5,column=1)button_forward.grid(row=5,column=2)defback(img_no):globallabelglobalbutton_forwardglobalbutton_backglobalbutton_exitlabel.grid_forget()label=Label(image=List_images[img_no-1])label.grid(row=1,column=0,columnspan=3)button_forward=Button(root,text="forward",command=lambda:forward(img_no+1))button_back=Button(root,text="Back",command=lambda:back(img_no-1))ifimg_no==1:button_back=Button(root,text="Back",state=DISABLED)label.grid(row=1,column=0,columnspan=3)button_back.grid(row=5,column=0)button_exit.grid(row=5,column=1)button_forward.grid(row=5,column=2)root=Tk()root.title("Image Viewer")root.geometry("700x700")image_no_1=ImageTk.PhotoImage(Image.open("Sample.png"))image_no_2=ImageTk.PhotoImage(Image.open("sample.png"))image_no_3=ImageTk.PhotoImage(Image.open("Sample.png"))image_no_4=ImageTk.PhotoImage(Image.open("sample.png"))List_images=[image_no_1,image_no_2,image_no_3,image_no_4]label=Label(image=image_no_1)label.grid(row=1,column=0,columnspan=3)button_back=Button(root,text="Back",command=back,state=DISABLED)button_exit=Button(root,text="Exit",command=root.quit)button_forward=Button(root,text="Forward",command=lambda:forward(1))button_back.grid(row=5,column=0)button_exit.grid(row=5,column=1)button_forward.grid(row=5,column=2)root.mainloop()

Output:


Image Viewer App in Python using Tkinter

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp