Timed Background Images – Python Tkinter GUI Tutorial 186

In this video I’ll show you how to automatically change the background image of your tkinter app using a timer.

Say you want to use images as the background of your tkinter app, but every X seconds you want the background image to change to something else.

I’ll show you how to do it in this video!

Check out my other video on background images:

https://bit.ly/3s2nsza

Python Code: timer_bg.py
(Github Code)

from tkinter import *

root = Tk()
root.geometry("1000x667")
root.title('Learn To Code at Codemy.com')
root.iconbitmap('c:/gui/codemy.ico')

global our_images, count
count = -1

our_images = [
	PhotoImage(file = "images/bg/1.png"),
	PhotoImage(file = "images/bg/2.png"),
	PhotoImage(file = "images/bg/3.png"),
]

# Create a Canvas
my_canvas = Canvas(root, width=1000, height=667, highlightthickness=0)
my_canvas.pack(fill="both", expand=True)

# Set the canvas image
my_canvas.create_image(0,0, image=our_images[0], anchor='nw')

def next():
	global count
	if count == 2:
		my_canvas.create_image(0,0, image=our_images[0], anchor='nw')		
		count = 0
	else:
		my_canvas.create_image(0,0, image=our_images[count+1], anchor='nw')	
		count += 1

	root.after(1000, next)

next()
mainloop()

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of...