每隔一段时间播放一次声音样本。文本输入

时间:2020-03-20 18:40:20

标签: python-3.x tkinter playsound-module

我需要在每个tkinter.text输入中播放一个打字机键样本。 我遇到了Playsound模块,但我不知道如何收听输入。

2 个答案:

答案 0 :(得分:1)

您将使用bind并设置一个函数来在绑定触发时播放声音。

import tkinter as tk

def key(event):
    print("pressed", repr(event.char))
    # Play sound here

root = tk.Tk()

text = tk.Text(root)
text.pack()
text.bind('<Key>', key)

root.mainloop()

答案 1 :(得分:0)

谢谢,我想出了一个非常相似的解决方案,因为它真的很慢。我基本上是在编写一个简单的打字机模拟器,以便为每个键入的字母重现按键声音。

import tkinter as tk
from PIL import Image, ImageTk
from playsound import playsound


def key(event):
    key = event.char
    playsound("C:/Users/Isma/key1.mp3")



win = tk.Tk()

frame = tk.Frame(win, width=300, height=400)
frame.grid(row=1, column=0)
text = tk.Text(frame)
text.grid(row=0,column=0)
text.bind('<Key>',lambda a : key(a))
image = Image.open("C:/Users/Isma/swintec1.jpg")
photo = ImageTk.PhotoImage(image)
label = tk.Label(frame,image=photo)
label.image = photo
label.grid(row=3,column=0)

win.mainloop()