在 python tkinter 中弹跳 DVD 徽标

问题描述 投票:0回答:2

我正在尝试在 tkinter 中制作一个弹跳的 DVD 徽标,但我真的不知道如何制作它,它与球配合使用,徽标不会移动。 标志

# 1) create main window
from tkinter import *
from PIL import ImageTk, Image
fen = Tk()
fen.title('AllTech - Bouncing ball')
fen.resizable(False, False)

# 2) create canvas and ball
WIDTH, HEIGHT = 400, 300
canvas = Canvas(fen, width=WIDTH, height=HEIGHT)
canvas.pack()

img = ImageTk.PhotoImage(Image.open("dvd.gif"))
# ball = canvas.create_oval(10, 10, 50, 50, fill='black')

# 3) move the ball
xspeed = yspeed = 3


frame = Frame(fen, width=600, height=400)
frame.pack()
frame.place(anchor='center', relx=0.5, rely=0.5)

label = Label(frame, image = img)
label.pack()


def moveBall():

    global xspeed, yspeed

    canvas.move(canvas, xspeed, yspeed)

    (leftPos, topPos, rightPos, bottomPos) = canvas.coords(img)

    if leftPos <= 0 or rightPos >= WIDTH:
        xspeed = -xspeed
    if topPos <= 0 or bottomPos >= HEIGHT:
        yspeed = -yspeed

    img.after(30, moveBall)


canvas.after(30, moveBall)
fen.mainloop()

我尝试使用球广告,它可以工作,但我不知道为什么,它没有徽标。

python image tkinter tkinter-canvas
2个回答
0
投票

您需要使用

canvas.create_image()
放置图像,然后可以使用
canvas.move()
移动图像。

from tkinter import *
from PIL import ImageTk, Image

fen = Tk()
fen.title('AllTech - Bouncing ball')
fen.resizable(False, False)

WIDTH, HEIGHT = 400, 300
canvas = Canvas(fen, width=WIDTH, height=HEIGHT, bg="white")
canvas.pack()

img = ImageTk.PhotoImage(Image.open("dvd.gif"))
# put the image into canvas
logo = canvas.create_image(0, 0, image=img, anchor="nw")

xspeed = yspeed = 3

def moveLogo():
    global xspeed, yspeed

    # move the image
    canvas.move(logo, xspeed, yspeed)

    # get bounding box of the image
    (leftPos, topPos, rightPos, bottomPos) = canvas.bbox(logo)

    if leftPos <= 0 or rightPos >= WIDTH:
        xspeed = -xspeed
    if topPos <= 0 or bottomPos >= HEIGHT:
        yspeed = -yspeed

    canvas.after(30, moveLogo)

canvas.after(30, moveLogo)
fen.mainloop()

enter image description here


0
投票

iejsndhf 嗬。艾乌梅亨尔 埃杰·德维埃 Wjw。 D DVD d2 西伯利亚.2 Wjw sjuwmwuw Wjwn

© www.soinside.com 2019 - 2024. All rights reserved.