Python - Simple Image Viewer
Submitted by razormist on Tuesday, March 20, 2018 - 22:39.
In this tutorial we will create a Simple Image Viewer using Python. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. So let do the coding.
There you have it we just created a Simple Image Viewer Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Getting started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.Importing Modules
After setting up the installation, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor- from tkinter import *
- from PIL import Image, ImageTk
Creating the Interface
This is the interface code for the Python application.The window will automatically generate when the program run. To do that just copy the code below then paste it inside the IDLE text editor.- root = Tk()
- root.title("Python: Simple Image Viewer")
- width = 600
- height = 300
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- root.geometry("%dx%d+%d+%d" % (width, height, x, y))
- root.resizable(0, 0)
Creating The Main Function
This is the Main Code for the Python application. The code contains class that has a several function that will be called. To do that just copy the code below then paste it inside the IDLE text editor.- #================================METHODS========================================
- def DisplayImage(event=None):
- Home = Toplevel()
- Home.title("Simple Image Viewer/Viewer")
- width = 800
- height = 600
- screen_width = Home.winfo_screenwidth()
- screen_height = Home.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
- Home.resizable(0, 0)
- load = Image.open('images/flower.jpg')
- load = load.resize((width, height), Image.ANTIALIAS)
- render = ImageTk.PhotoImage(load)
- panel = Label(Home, image=render)
- panel.image=render
- panel.pack(fill=BOTH, expand=YES)
- #================================FRAMES=========================================
- Top = Frame(root, width=600, bd=1, relief=SOLID)
- Top.pack(side=TOP)
- Mid = Frame(root, width=300, height=200, bd=1, relief=SOLID)
- Mid.pack_propagate(0)
- Mid.pack(pady=20)
- #================================LABEL WIDGETS==================================
- lbl_title = Label(Top, text="Python: Simple Image Viewer", width=600, font=("arial", 20))
- lbl_title.pack(fill=X)
- lbl_text = Label(Mid, text="flower.jpg", font=("arial", 20))
- lbl_text.bind("<Button-1>", DisplayImage)
- lbl_text.pack()
Initializing the Application
This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.- #================================INITIALIZATION=================================
- if __name__ == '__main__':
- root.mainloop()
Add new comment
- 1266 views