import sounddevice as sd
import pyautogui
import threading
import numpy as np
import win32gui
import winsound
import time
def zd():
filename = '123' #文件名
def close_process_by_hwnd(hwnd, _):
if win32gui.IsWindowVisible(hwnd):
# 可以正则判断
if filename in win32gui.GetWindowText(hwnd):
win32gui.SetForegroundWindow(hwnd) #使当前窗口在最前 (窗口不可以最小化)
if __name__ == '__main__':
win32gui.EnumWindows(close_process_by_hwnd, None)
zd()
# volume_norm = 100
def ui():
time.sleep(0.5)
pyautogui.hotkey("ctrl", "g")
time.sleep(1)
pyautogui.press('1')
def target():
def print_sound(indata, outdata, frames, time, status):
global volume_norm #修改内容
volume_norm = np.linalg.norm(indata)*10
# print("|" * int(volume_norm))
if int(volume_norm) > 8:
print(int(volume_norm))
ui()
while True:
# while time.time() + 3600*24:
with sd.Stream(callback=print_sound):
sd.sleep(10000*10000)
## 属于线程t的部分
t = threading.Thread(target=target)
t.start()
a = 0
while True:
# print('开始')
while True:
# print('等待')
time.sleep(1)
a = a + 1
# print(a)
if int(volume_norm) < 8:
# print(int(volume_norm))
pass
if int(volume_norm) > 8:
a = 0
break
if a > 20:
pyautogui.press('1')
a = 0
break
修改了 global volume_norm 全局变量和判断语句
感觉方式很笨拙
自学 有更好的方法请赐教