【发布时间】:2014-11-27 04:35:21
【问题描述】:
如何在 Tkinter 的 GUI 上按下按钮时获得音效?
这是我的代码:
from Tkinter import *
root = Tk() #root object for the buttons
from PIL import Image, ImageTk #python imaging library
#open the images and store them in photos
image = Image.open("Jolteon.PNG")
image1 = Image.open("Eevee.PNG")
image2 = Image.open("Vaporeon.PNG")
image3 = Image.open("Flareon.PNG")
photo = ImageTk.PhotoImage(image)
photo1 = ImageTk.PhotoImage(image1)
photo2 = ImageTk.PhotoImage(image2)
photo3 = ImageTk.PhotoImage(image3)
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root) #some different frames
bottomFrame.pack(side=BOTTOM)
button1 = Button(topFrame, text="Eevee", fg="brown")
button2 = Button(topFrame, text="Jolteon", fg="yellow")
button3 = Button(topFrame, text="Vaporeon", fg="blue")
button4 = Button(topFrame, text="Flareon", fg="red")
button5 = Button(topFrame,image=photo)
button6 = Button(topFrame,image=photo1)
button7 = Button(topFrame,image=photo2) #sdbsdfbdfsbdfb
button8 = Button(topFrame,image=photo3)
#packages the buttons so that it can be produced
button1.pack()
button6.pack()
button2.pack() #sdbsdbsdbsdfbfdsn
button5.pack()
button3.pack()
button7.pack()
button4.pack()
button8.pack()
root.mainloop()
它显示来自 Pokemon 的 Eevee trios 的名称和图片。
我想要的是当我按下口袋妖怪的图片让口袋妖怪哭泣时。
我将如何继续实施?
【问题讨论】:
-
更多关于播放声音的信息可以在here找到。如果您有一个播放声音的函数(比如
eeveeSound()),只需使用Button的command=eeveeSound选项将其绑定到该函数。
标签: python image user-interface audio tkinter