我正在使用Python GUI tkinter创建一个太阳系。我必须在其中绕着太阳移动行星。我正在使用行星图像来显示行星。我创造了一颗绕太阳移动的行星。但是在这里我遇到一个问题,我想在地球上添加悬停效果(图片)。
我尝试了一些代码,但是没有用。 当我们在地球上放箭头时,我想创建一个悬停效果,以便改变星球的图像;当我移除它时,可以用最旧的图像替换它。
这是代码:
import math
try:
import tkinter as tk
except ImportError:
import Tkinter as tk # Python 2
DELAY = 100
CIRCULAR_PATH_INCR = 10
sin = lambda degs: math.sin(math.radians(degs))
cos = lambda degs: math.cos(math.radians(degs))
class Celestial(object):
# Constants
COS_0, COS_180 = cos(0), cos(180)
SIN_90, SIN_270 = sin(90), sin(270)
def __init__(self, x, y, radius):
self.x, self.y = x, y
self.radius = radius
def bounds(self):
""" Return coords of rectanglesurrounding circlular object. """
return (self.x + self.radius*self.COS_0, self.y + self.radius*self.SIN_270,
self.x + self.radius*self.COS_180, self.y + self.radius*self.SIN_90)
def circular_path(x, y, radius, delta_ang, start_ang=0):
""" Endlessly generate coords of a circular path every delta angle degrees. """
ang = start_ang % 360
while True:
yield x + radius*cos(ang), y + radius*sin(ang)
ang = (ang+delta_ang) % 360## Heading ##
def update_position(canvas, id, celestial_obj, path_iter):
celestial_obj.x, celestial_obj.y = next(path_iter) # iterate path and set new position
# update the position of the corresponding canvas obj
x0, y0= canvas.coords(id) # coordinates of canvas oval object
oldx, oldy = (x0), (y0) # current center point
dx, dy = celestial_obj.x - oldx, celestial_obj.y - oldy # amount of movement
canvas.move(id, dx, dy) # move canvas oval object that much
# repeat after delay
canvas.after(DELAY, update_position, canvas, id, celestial_obj, path_iter)
def on(e):
planet1['image']=image2
def of(e):
planet1['image']=filename
top = tk.Tk()
top.title('Circular Path')
canvas = tk.Canvas(top, bg='black', height=500, width=500)
canvas.pack()
sol_obj = Celestial(250, 250, 25)
planet_obj1 = Celestial(250+100, 250, 15)
sol = canvas.create_oval(sol_obj.bounds(), fill='yellow', width=0)
filename = tk.PhotoImage(file = "moon.png")
planet1 = canvas.create_image(planet_obj1.bounds()[0],planet_obj1.bounds()[1],image=filename)
canvas.tag_bind(planet1,"<Enter>",on)
canvas.tag_bind(planet1,"<Leave>",of)
orbital_radius = math.hypot(sol_obj.x - planet_obj1.x, sol_obj.y - planet_obj1.y)
path_iter = circular_path(sol_obj.x, sol_obj.y, orbital_radius, CIRCULAR_PATH_INCR)
next(path_iter) # prime generator
top.after(DELAY, update_position, canvas, planet1, planet_obj1, path_iter)
top.mainloop()
如果有人知道如何解决,请快速答复。