vega2015
vega2015

Reputation: 139

Overlaying Images In tkinter

I'm just now entering the world of tkinter and it seems a bir confusing. To start off i've been trying to make an analog clock, to do this i've made 3 images wich will each overlay on top of eachother, the problem is I can't seem to make all three visible at once. I thought of pasting each onto a blank image and then creating that image but I think there has to be a better way. This is what I have so far:

import time
from tkinter import *
from PIL import Image,ImageTk
root = Tk()
root.geometry ("1000x700+750+200")
root.title("Analog Clock")
seconds = int(time.strftime("%S"))
minutes = int(time.strftime("%M"))
hours = int(time.strftime("%H"))
if hours>12:
    hours=hours-12
secondssize=(100,100)
secondsangle = 6*seconds
minutessize=(100,300)
minutesangle = 6*minutes
hourssize=(100,200)
hoursangle = 30*hours
space = Canvas(root, width=500, height=500, bg="white")
space.pack()
im = Image.open("pointer.jpg")
im = im.convert('RGBA')
im2 = im.copy()
im3 = im.copy()
secondsimage = im.rotate(-secondsangle, expand=1).resize(secondssize)
secondsimage = ImageTk.PhotoImage(secondsimage)
space.create_image(250, 250, image=secondsimage)
minutesimage = im2.rotate(-minutesangle, expand=1).resize(minutessize)
minutesimage = ImageTk.PhotoImage(minutesimage)
space.create_image(250, 250, image=minutesimage)
hoursimage = im3.rotate(-hoursangle, expand=1).resize(hourssize)
hoursimage = ImageTk.PhotoImage(hoursimage)
space.create_image(250, 250, image=hoursimage)
root.mainloop()

Upvotes: 1

Views: 612

Answers (1)

aless80
aless80

Reputation: 3332

For the arms you could use the create_line method:

space.create_line((250, 250, 250, 10), fill="black", width=3)

but you want an animation you might want to look at other examples and go from there, e.g. http://effbot.org/zone/tkinter-animation.htm

Upvotes: 1

Related Questions