python写一个敲木鱼加功德(加音效和敲击动作)

界面展示及视频演示

python写一个敲木鱼加功德(加音效和敲击动作)_第1张图片

1、先做一个基本界面

import tkinter
from PIL import Image, ImageTk # pip install pillow
 
# 界面
top=tkinter.Tk()
top.title('敲木鱼加功德')
top.geometry('410x400')
top.configure(bg='black')
 
# 准备图片
qiaomuyutupian=ImageTk.PhotoImage(file='敲木鱼.jpg') # 转化为tkinter可以使用的图片
 
# 初始化功德
gongde=0
 
# 标签
label1=tkinter.Label(top,text='积攒功德:'+str(gongde),font=('华文新魏',15),fg='white',bg='black',width=18)
label1.place(x=100,y=70)
 
# 方法
def qiaomuyu():
        # 设gongde为全局变量,并更新标签
        global gongde
        gongde=gongde+1
 
# 按钮
button1=tkinter.Button(top,image=qiaomuyutupian,relief='ridge',command=qiaomuyu)
button1.place(x=100,y=100)
 
top.mainloop()

2、用pygame添加声音最简单,并用多线程启动(这样不用等声音播放完就可以继续按了)

import tkinter
import threading
import pygame # pip install pygame
from PIL import Image, ImageTk # pip install pillow
 
# 准备音频
pygame.mixer.init()
pygame.mixer.music.load('敲.mp3')
 
# 界面
top=tkinter.Tk()
top.title('敲木鱼加功德')
top.geometry('410x400')
top.configure(bg='black')
 
# 准备图片
qiaomuyutupian=ImageTk.PhotoImage(file='敲木鱼.jpg') # 转化为tkinter可以使用的图片
 
# 初始化功德
gongde=0
 
# 标签
label1=tkinter.Label(top,text='积攒功德:'+str(gongde),font=('华文新魏',15),fg='white',bg='black',width=18)
label1.place(x=100,y=70)
 
# 方法
def qiaomuyu():
        # 设gongde为全局变量,并更新标签
        global gongde
        gongde=gongde+1
        label1.config(text='积攒功德:'+str(gongde))
 
        # 多线程启动解决延时,虽然延迟足够小,但为了更有效果
        th=threading.Thread(target=pygame.mixer.music.play)
        th.start()
 
# 按钮
button1=tkinter.Button(top,image=qiaomuyutupian,relief='ridge',command=qiaomuyu)
button1.place(x=100,y=100)
 
top.mainloop()

3、添加上浮移动的文字。由于实在不能把控件设置成透明色,所以用个背景色为黑色的Text控件,只要逐行删除就有效果了,同样多线程启动

import time
import tkinter
import threading
import pygame # pip install pygame
from PIL import Image, ImageTk # pip install pillow
 
# 准备音频
pygame.mixer.init()
pygame.mixer.music.load('敲.mp3')
 
# 界面
top=tkinter.Tk()
top.title('敲木鱼加功德')
top.geometry('410x400')
top.configure(bg='black')
 
# 准备图片
qiaomuyutupian=ImageTk.PhotoImage(file='敲木鱼.jpg') # 转化为tkinter可以使用的图片
 
# 初始化功德
gongde=0
 
# 标签
label1=tkinter.Label(top,text='积攒功德:'+str(gongde),font=('华文新魏',15),fg='white',bg='black',width=18)
label1.place(x=100,y=70)
 
def showplus():
        for i in range(4):
                text1.insert('insert',' \n')
        else:
                text1.insert('insert',' 功德 + 1')
 
        for i in range(5):
                time.sleep(0.03)
                text1.delete(1.0, 2.0)
 
# 方法
def qiaomuyu():
        # 设gongde为全局变量,并更新标签
        global gongde
        gongde=gongde+1
        label1.config(text='积攒功德:'+str(gongde))
 
        # 多线程启动解决延时,虽然延迟足够小,但为了更有效果
        th=threading.Thread(target=pygame.mixer.music.play)
        th.start()
 
        th2=threading.Thread(target=showplus)
        th2.start()
 
# 按钮
button1=tkinter.Button(top,image=qiaomuyutupian,relief='ridge',command=qiaomuyu)
button1.place(x=100,y=100)
 
text1=tkinter.Text(top,width=10,height=5,bg='black',bd=0,foreground='white')
text1.place(x=125,y=115)
top.mainloop()

4、P一张敲到木鱼的图片,两张图片来回切换就有动画效果了,同样多线程启动

import time
import tkinter
import threading
import pygame # pip install pygame
from PIL import Image, ImageTk # pip install pillow
 
# 准备音频
pygame.mixer.init()
pygame.mixer.music.load('敲.mp3')
 
# 界面
top=tkinter.Tk()
top.title('敲木鱼加功德')
top.geometry('410x400')
top.configure(bg='black')
 
# 准备图片
qiaomuyutupian=ImageTk.PhotoImage(file='敲木鱼.jpg') # 转化为tkinter可以使用的图片
qiaomuyutupian2=ImageTk.PhotoImage(file='敲木鱼2.jpg') # 转化为tkinter可以使用的图片
 
# 初始化功德
gongde=0
 
# 标签
label1=tkinter.Label(top,text='积攒功德:'+str(gongde),font=('华文新魏',15),fg='white',bg='black',width=18)
label1.place(x=100,y=70)
 
def showplus():
        for i in range(4):
                text1.insert('insert',' \n')
        else:
                text1.insert('insert',' 功德 + 1')
 
        for i in range(5):
                time.sleep(0.03)
                text1.delete(1.0, 2.0)
 
def changetupian():
        button1.config(image=qiaomuyutupian2)
        time.sleep(0.1)
        button1.config(image=qiaomuyutupian)
 
# 方法
def qiaomuyu():
        # 设gongde为全局变量,并更新标签
        global gongde
        gongde=gongde+1
        label1.config(text='积攒功德:'+str(gongde))
 
        # 多线程启动解决延时,虽然延迟足够小,但为了更有效果
        th=threading.Thread(target=pygame.mixer.music.play)
        th.start()
 
        th2=threading.Thread(target=showplus)
        th2.start()
 
        th3=threading.Thread(target=changetupian)
        th3.start()
 
# 按钮
button1=tkinter.Button(top,image=qiaomuyutupian,relief='ridge',command=qiaomuyu)
button1.place(x=100,y=100)
 
text1=tkinter.Text(top,width=10,height=5,bg='black',bd=0,foreground='white')
text1.place(x=125,y=115)
 
 
top.mainloop()

你可能感兴趣的:(Python,python,开发语言)