IT Learning

実践形式でITのお勉強

Python

【Python】Changing images periodically with tkinter

投稿日:

Overview

There is a good library in python to make GUI, that is tkinter. It can display images too. Here, we are trying to change images periodically with tkinter.

Python library to be used

Following three libraries are used.

  • tkinter

‘tkinter’ is used to make GUI.

  • PIL(pillow)

‘PIL’ is used to deal with images in the python.

  • threading

‘threading’ is used to change images automatically. Detail about that will be explained later.

By the way, if you are using anaconda which contains these three libraries already , you don’t have to install them with pip command.

Step1 : Display a image with tkintertkinter

At first, we are defining a function to show a image with tkinter.

# import libraries
import sys
import tkinter
from PIL import Image, ImageTk
import time

# Definition of a function to display image
def show_image():
  #Making instance of tkinter
    root = tkinter.Tk()
    root.title('test')
    root.geometry("400x300")

  # Opening image(Please change your actual path to your image)
    img = Image.open('yourimage.jpg')
    img = ImageTk.PhotoImage(img)

  # Definition of Canvas
    canvas = tkinter.Canvas(bg = "black", width=400, height=300)
    canvas.place(x=100, y=50)
    item = canvas.create_image(30, 30, image=img, anchor=tkinter.NW)

  # Display
    root.mainloop()

This function displays image by using canvas method of tkinter. ImageTK is necessary to use jpg file.

Step2 : Changing images

The purpose of here is changing some images automatically with using the function before we defined. But that function seems to have the problem that only some objects on GUI can control tkinter when it’s executing root.mainloop. To change images periodically and automatically without GUI objects, we ‘re using threading library like following.

import sys
import tkinter
from PIL import Image, ImageTk
import threading
import time

def show_image():
  
    # Definition as global to be controlled out of the function
    global item, canvas

    root = tkinter.Tk()
    root.title('test')
    root.geometry("400x300")
    img = Image.open('your_image.jpg')
    img = ImageTk.PhotoImage(img)
    canvas = tkinter.Canvas(bg = "black", width=400, height=300)
    canvas.place(x=100, y=50)
    item = canvas.create_image(30, 30, image=img, anchor=tkinter.NW)
    root.mainloop()

# make a thread and start to display a image
thread1 = threading.Thread(target=show_image)
thread1.start()

# Definition of a image to be changed
img2 = Image.open('your_image2.jpg')
img2 = ImageTk.PhotoImage(img2)
time.sleep(3) # cyclic 3 seconds

#Rewrite the item
canvas.itemconfig(item,image=img2)
time.sleep(3)

#Rewrite the item again
img = Image.open('your_image.jpg')
img = ImageTk.PhotoImage(img)
canvas.itemconfig(item,image=img)

This code can display a first image in thread and change it to second one after 3 seconds.

Summary

It may not the best way but it can realize what we want to do by using threading and rewrite the item out of the function.

Related

-Python

執筆者:


comment

Your email address will not be published. Required fields are marked *