今天七夕節,外面下着大雨,用Python的tkinter做一個下愛心雨的特效,發給妹子


今天七夕,還下着雨,剛好想做一個下着愛心雨的特效

准備圖片素材

1、美圖秀秀找一個愛心圖,大小就50*50就可以,生成的是一個png格式文件

2、由於canvas.create_image只支持gif圖片,所以在線轉換一下,我這里用的是我拉網:http://pic.55.la/

創建畫布,畫布添加愛心圖,圖片下落,使用多線程(由於雨要一直下)

Python好難寫,調試了半天,話不多說,看看小白初步實現的代碼,關鍵地方加了注釋

# -*- coding:utf-8 -*-
# __author__ :kusy
# __content__:文件說明
# __date__:2018/8/17 9:28


from tkinter import *
import random
import threading
import time
import os

# 初始雨滴縱坐標
INIT_HEIGHT = 10

# 雨滴創建
def rainmake(canvas,imagefile):
    rainlist = []
    for i in range(10):
        # 根據圖片,創建一排心
        rainlist.append(canvas.create_image(100 + 80 * i, INIT_HEIGHT, anchor=NE, image=imagefile))
    return rainlist

# 雨滴下落
def raindown(tk,canvas,imagefile,sec):
    #線程間等待
    time.sleep(sec)
    rainlist = rainmake(canvas,imagefile)

    # 每顆心的縱坐標值
    height = [INIT_HEIGHT] * 10
    while True:
        # 每次移動前稍等一會
        time.sleep(0.2)

        # 10顆心一起移動
        for i in range(10):
            # 如果這顆心到底了,則不繼續移動,否則height重置就無效了
            if not height[i] == 0:
                # 設置下落步調
                rnd = random.randint(5,50)
                canvas.move(rainlist[i],0,rnd)
                height[i] = height[i] + rnd
                tk.update()

        for i,h in enumerate(height):
            if h > 600:
                # 當這顆心走到最下方,則刪除
                canvas.delete(rainlist[i])
                tk.update()
                # 清空這顆心的height
                height[i] = 0
                print(i,h,height)

        # 10顆心全到底,則跳出循環
        # print(height,height == [0] * 10)
        if height == [0] * 10:
            print('break:',threading.current_thread().name)
            break

def lookloop(tk,canvas,thread):
    aliveflg = False
    while True:
        # 5s檢測一次
        time.sleep(5)
        for th in thread:
            if th.is_alive():
                aliveflg = True
            else:
                aliveflg = False

        if aliveflg == False:
            break
    #Over
    canvas.create_text(200,300,text='不好意思,雨停了...',fill='red')
    canvas.pack()
    time.sleep(5)
    tk.destroy()

def main():
    # 創建窗口對象
    tk = Tk()
    tk.title('七夕之雨')

    canvas_style = {
        'bg':'white',
        'height':'700',
        'width':'900',
        'cursor':'circle'
    }
    # 創建畫布
    canvas = Canvas(tk,canvas_style)
    canvas.pack()
    # 圖片素材
    if not os.path.exists('7777777.gif'):
        raise Exception('7777777.gif file does not exists.')
    imagefile = PhotoImage(file = "7777777.gif")

    thread = []
    for i in range(10):
       thread.append(threading.Thread(target=raindown,args=(tk,canvas,imagefile,i)))
    for t in thread:
        t.start()

    # 新開一個線程監控運行中的10個線程
    threading.Thread(target=lookloop,args=(tk,canvas,thread)).start()

    # 進入消息循環
    tk.mainloop()

if __name__ == '__main__':
    main()

動態效果圖如下 

 

最重要的一步:發給妹子

(當然要發給妹子,不然做這個干啥)可以打包成exe文件,和素材圖片一起發給妹子。。。

PS:此處附上已打包好的文件:鏈接:https://pan.baidu.com/s/1WBnnkit_k1fojHHnnDeJvw 密碼:v8ah

 

我的博客即將搬運同步至騰訊雲+社區,邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=1ntsxvl0xbssw


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM