美文网首页
Python-141 用pillow,playsound和tki

Python-141 用pillow,playsound和tki

作者: RashidinAbdu | 来源:发表于2022-04-24 11:29 被阅读0次
    • 此前用python写过一些自己会用到的工具,然后前几天想着儿子马上要三个月了,再加自己远在他乡,并不能在家照顾他,就给他买了一些书,然后媳妇儿每天给他念一些。就想起,要不自己给他做个小的Cartoon Alphabet, 然后加上picture, audio和lullaby,这样只需要点一下就可以给他念出来对应图片上的动物或其他物件的名字,并跳出对应的字母,告诉他:这是什么!此外,回头有空了再给这个程序加上:点击图片上播放按钮,给他播放各种好玩的儿歌,比如BINGO啥的。


      image.png
    • 需要事先安装tkinter, pillow, playsound

    • 主要是:给按钮加了图片,然后加了多个动作,即:除了点击时播放声音外,还会跳出字段,可能儿子哪天能拼读了就有用了哈哈


    • 直接放代码:
    
    from tkinter import *
    from tkinter import messagebox
    from PIL import ImageTk ,Image
    from playsound import playsound
    
    tk = Tk()
    tk.title("My Son Jasur's Alphabet")
    tk.geometry("600x800")
    # 参考,还没弄完 0413-2053
    img=ImageTk.PhotoImage(Image.open ("C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/SON.jpg"))
    lab=Label(image=img)
    lab.pack()
    
    button=Button(tk,text='exit',command=tk.quit)
    button.pack()
    # making function
    
    #read the image
    #photo1 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/apple.png")
    im1 = Image.open("C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/APPLE.png")
    #im2 = Image.open("C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/apple.png")
    im2 = Image.open("C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/STAR.png")
    im3 = Image.open("C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/PLAY.png")
    #im2 = Image.open("C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/apple.png")
    im4 = Image.open("C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/GARDEN.png")
    #show images
    photo1 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/APPLE.png")
    photo7 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/STAR.png")
    photo3 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/PLAY.png")
    photo4 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/GARDEN.png")
    photo5 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/SPIDER.png")
    photo6 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/BOOK.png")
    photo2 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/PEAR.png")
    photo8 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/HAT.png")
    
    photo11 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/HOUSE.png")
    photo12 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/ROOSTER.png")
    photo13 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/PEN.png")
    photo14 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/CAT.png")
    photo15 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/FISH.png")
    photo16 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/CAMEL.png")
    photo17 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/EAGLE.png")
    photo18 = PhotoImage(file=r"C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_Fig/MIRROR.png")
    
    photoimage1 = photo1.subsample(4, 4)
    photoimage2= photo2.subsample(4, 4)
    photoimage3 = photo3.subsample(4, 4)
    photoimage4= photo4.subsample(4, 4)
    photoimage5= photo5.subsample(4, 4)
    photoimage6= photo6.subsample(4, 4)
    photoimage7= photo7.subsample(4, 4)
    photoimage8= photo8.subsample(4, 4)
    
    photoimage11 = photo11.subsample(4, 4)
    photoimage12= photo12.subsample(4, 4)
    photoimage13 = photo13.subsample(4, 4)
    photoimage14= photo14.subsample(4, 4)
    photoimage15= photo15.subsample(4, 4)
    photoimage16= photo16.subsample(4, 4)
    photoimage17= photo17.subsample(4, 4)
    photoimage18= photo18.subsample(4, 4)
    
    def show1():
        im1.show()
    def show2():
        im2.show()
    def show3():
        im1.show()
    def show4():
        im2.show()
    def show5():
        im2.show()
    def show6():
        im2.show()
    def show7():
        im2.show()
    def show8():
        im2.show()
    #im1.show()
    #im2.show()
    
    def play1():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/APPLE1.MP3')
    def play7():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/STAR1.mp3')
    def play3():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/FARM1.mp3')
    def play4():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/GARDEN.mp3')
    def play5():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/SPIDER1.mp3')
    def play6():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/BOOK1.mp3')
    def play2():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/PEAR.mp3')
    def play8():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/HAT.mp3')
    
    def play11():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/House.MP3')
    def play12():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/ROOSTER.mp3')
    def play13():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/PEN.mp3')
    def play14():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/CAT.mp3')
    def play15():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/FISH.mp3')
    def play16():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/CAMEL.mp3')
    def play17():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/EAGLE.mp3')
    def play18():
        playsound('C:/Users/Rashidin Abdugheni/PycharmProjects/Jasur-Alphabet/JasurAlphabet_App/MIRROR.mp3')
    
    def show_msg1():
       messagebox.showinfo("Apple",".بۇ بىر ئالما")
    def show_msg7():
       messagebox.showinfo("Star","Bu bolsa yultuz.")
    def show_msg3():
       messagebox.showinfo("Game","Bu bolsa Oyunlar.")
    def show_msg4():
       messagebox.showinfo("Garden","Bu bolsa Baghcha.")
    def show_msg5():
       messagebox.showinfo("Garden","Bu bolsa Omuchuk.")
    def show_msg6():
       messagebox.showinfo("Garden","Bu bolsa Kitab.")
    def show_msg2():
       messagebox.showinfo("Garden","Bu bolsa Neshput.")
    def show_msg8():
       messagebox.showinfo("Garden","Bu bolsa Doppa.")
    
    def show_msg11():
       messagebox.showinfo("House",".Bu bir Oy")
    def show_msg12():
       messagebox.showinfo("Star","Bu bir Xoraz.")
    def show_msg13():
       messagebox.showinfo("Game","Bu bir Qelem.")
    def show_msg14():
       messagebox.showinfo("Garden","Bu bir Mushuk.")
    def show_msg15():
       messagebox.showinfo("Garden","Bu bir Beliq.")
    def show_msg16():
       messagebox.showinfo("Garden","Bu bir Toge.")
    def show_msg17():
       messagebox.showinfo("Garden","Bu bir Burkut.")
    def show_msg18():
       messagebox.showinfo("Garden","Bu bir Eynek.")
    
    def task(event):
        print("Hello")
    def quit(event):
        print("Double click  to stop")
        import sys; sys.exit()
    
    #def Close(): play1().destroy()
    #using multi actions in one button text="Button", command=lambda: [fun1(), fun2()])
    #b1 = Button(tk, text="Apple", command=lambda: [show1(), play()].place(x=100, y=100)
    b1 = Button(tk, text="Apple", image=photoimage1, relief=GROOVE,command=lambda: [play1(), show_msg1()]).place(x=5, y=10)
    b2 = Button(tk, text="Star",  font=("Helvetica", 32), relief=GROOVE,image=photoimage2,command=lambda: [play2(),show_msg2()]).place(x=180, y=10)
    b3 = Button(tk, text="Play", image=photoimage3, relief=GROOVE,command=lambda: [play3(),show_msg3()]).place(x=5, y=170)
    b4 = Button(tk, text="Garden",  font=("Helvetica", 32), relief=GROOVE,image=photoimage4,command=lambda: [play4(),show_msg4()]).place(x=180, y=170)
    b5 = Button(tk,  font=("Helvetica", 32), relief=GROOVE,image=photoimage5,command=lambda: [play5(),show_msg5()]).place(x=5, y=330)
    b6 = Button(tk,  font=("Helvetica", 32), relief=GROOVE,image=photoimage6,command=lambda: [play6(),show_msg6()]).place(x=180, y=330)
    b7 = Button(tk,  font=("Helvetica", 32), relief=GROOVE,image=photoimage7,command=lambda: [play7(),show_msg7()]).place(x=5, y=490)
    b8 = Button(tk,  font=("Helvetica", 32), relief=GROOVE,image=photoimage8,command=lambda: [play8(),show_msg8()]).place(x=180, y=490)
    
    b11 = Button(tk, text="Apple", image=photoimage11, relief=GROOVE,command=lambda: [play11(), show_msg11()]).place(x=930, y=10)
    b12 = Button(tk, text="Star",  font=("Helvetica", 32), relief=GROOVE,image=photoimage12,command=lambda: [play12(),show_msg12()]).place(x=1105, y=10)
    b13 = Button(tk, text="Play", image=photoimage13, relief=GROOVE,command=lambda: [play13(),show_msg13()]).place(x=930, y=170)
    b14 = Button(tk, text="Garden",  font=("Helvetica", 32), relief=GROOVE,image=photoimage14,command=lambda: [play14(),show_msg14()]).place(x=1105, y=170)
    b15 = Button(tk,  font=("Helvetica", 32), relief=GROOVE,image=photoimage15,command=lambda: [play15(),show_msg15()]).place(x=930, y=330)
    b16 = Button(tk,  font=("Helvetica", 32), relief=GROOVE,image=photoimage16,command=lambda: [play16(),show_msg16()]).place(x=1105, y=330)
    b17 = Button(tk,  font=("Helvetica", 32), relief=GROOVE,image=photoimage17,command=lambda: [play17(),show_msg17()]).place(x=930, y=490)
    b18 = Button(tk,  font=("Helvetica", 32), relief=GROOVE,image=photoimage18,command=lambda: [play18(),show_msg18()]).place(x=1105, y=490)
    tk.mainloop()
    
    • 点解图片的话,会念出图片的名字,并给出对应的This is Blabla!
    • 运行结果:


      image.png

    相关文章

      网友评论

          本文标题:Python-141 用pillow,playsound和tki

          本文链接:https://www.haomeiwen.com/subject/rvltyrtx.html