python - tkinter 不需要的函数覆盖

标签 python function animation canvas tkinter

所以基本上我有一个带有 1 个对象的动画的函数,并且我希望能够拥有更多独立的对象,但是当我再次调用该函数时,它只会覆盖旧的对象,并且我希望能够拥有多个对象(我想要下雨)

import  random
import tkinter
canvas = tkinter.Canvas(width=1000, height=600, bg="white")
canvas.pack()

x = 0
y = 0

def idk():
    idk2()
    canvas.after(2000,idk2)

def idk2():
    global x
    x = random.randint(0,1000)
    idk3()

def idk3():
    global y
    y = y+10
    canvas.delete('idk3')
    canvas.create_rectangle(x-2,y-10,x+2,y+10,tag="idk3")
    if y<600:
        canvas.after(50, idk3)

idk()

最佳答案

如果你想要大量下雨,你需要在雨滴到达地面之前保存它们。

下面是一个例子:

raindrops = {}

def raining():
    # update existing rain drops
    for rd in list(raindrops):
        if raindrops[rd] < 600:
            # keep rain falling
            raindrops[rd] += 10
            canvas.move(rd, 0, 10)
        else:
            # rain drop reaches the ground, remove it
            canvas.delete(rd)
            del raindrops[rd]
    # create new rain drop at random x
    x = random.randint(0, 1000)
    rd = canvas.create_line(x, 0, x, 20)
    raindrops[rd] = 0  # save the y of rain drop
    canvas.after(20, raining)

关于python - tkinter 不需要的函数覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60403251/

相关文章:

python - 我可以从命令行将 "\t"传递给 python 吗?

Python 错误 : AttributeError: 'module' object has no attribute

javascript - 我的 javascript 函数陷入了循环......

android - 在 dialogFragment 中自动启动可绘制动画

javascript - CSS 动画被阻止

ios - 使动画播放/暂停按钮像顶空应用一样

python - Selenium 网络驱动程序 : How to Download a PDF File with Python?

python - 'ServiceAccountCredentials.from_json_keyfile_name' 相当于远程 json

c# - 在 JavaScript 中引用 C# 变量

python-3.x - 如何制作杂货收据程序