import os import random import time from fnmatch import fnmatch import pygame import tkinter as tk from tkinter import * import wave import threading import tkinter import tkinter.filedialog import tkinter.messagebox import pyaudio from urllib import request, parse import json root = tk.Tk() root.geometry("450x250+374+182") root.title("英语单词") english1 = "开始" res = "中文翻译" w = Message(root, font=('times', 15, 'bold'), text=english1) w.pack() timer_running = False a = Message(root, font=('times', 15, 'bold'), text=res) a.pack() def wands(): w.after(2000, word) time.sleep(3) w.after(2000, speak) def word(): path = "D:\MY python\English" lists = os.listdir(path) english = (random.choice(lists)) global english1 english1 = english[:-4] w.configure(text=f"{english1}") res = fy(f"{english1}") a.configure(text=f"{res}") def speak(): basedir = r"D:\MY python\English" for root, dirs, files in os.walk(basedir): for file in files: english3 = os.path.join(root, file) if fnmatch(file, f"{english1}*.wav"): pygame.mixer.init() pygame.mixer.music.load(english3) pygame.mixer.music.play() wands() 添加一个停止按钮
时间: 2024-04-16 08:25:07 浏览: 120
可以使用tkinter库来添加一个停止按钮。下面是修改后的代码:
```python
import os
import random
import time
from fnmatch import fnmatch
import pygame
import tkinter as tk
from tkinter import *
import wave
import threading
import tkinter
import tkinter.filedialog
import tkinter.messagebox
import pyaudio
from urllib import request, parse
import json
root = tk.Tk()
root.geometry("450x250+374+182")
root.title("英语单词")
english1 = "开始"
res = "中文翻译"
w = Message(root, font=('times', 15, 'bold'), text=english1)
w.pack()
timer_running = False
a = Message(root, font=('times', 15, 'bold'), text=res)
a.pack()
def wands():
w.after(2000, word)
time.sleep(3)
w.after(2000, speak)
def word():
path = "D:\MY python\English"
lists = os.listdir(path)
english = (random.choice(lists))
global english1
english1 = english[:-4]
w.configure(text=f"{english1}")
res = fy(f"{english1}")
a.configure(text=f"{res}")
def speak():
basedir = r"D:\MY python\English"
for root, dirs, files in os.walk(basedir):
for file in files:
english3 = os.path.join(root, file)
if fnmatch(file, f"{english1}*.wav"):
pygame.mixer.init()
pygame.mixer.music.load(english3)
pygame.mixer.music.play()
wands()
def stop_speak():
pygame.mixer.music.stop()
def fy(english):
content = english
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {}
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '16245570821515'
data['sign'] = '0f4c0f7c60570f9d9b8b62b9b7e3e65f'
data['lts'] = '1624557082151'
data['bv'] = 'e9df4b2b6a5c5a4fdec4a4d3c3e3e7ca'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTION'
data['typoResult'] = 'false'
data = parse.urlencode(data).encode('utf-8')
response = request.urlopen(url, data)
html = response.read().decode('utf-8')
target = json.loads(html)
return target['translateResult'][0][0]['tgt']
stop_button = Button(root, text="停止", command=stop_speak)
stop_button.pack()
wands()
root.mainloop()
```
阅读全文