Написал код, встроил его в трей от wxPython (при нажатии пункта в трее запускается код), но он содержит в себе цикл while isWorking == 1 из-за чего пока одна он не завершится, не получиться открыть трей (т.к в wxPython трей открывается через функцию ) и изменить значение isWorking на 0 тоже, т.к в моём коде оно меняется тоже через нажатие пункта в трее. Как можно реализовать паралельное выполнение нескольких функций и получение значения переменной 1-ой функцией из другой?
Код:
import keyboard as kb
import soundfile as sf
import soundcard as sc
import wx
import wx.adv
TRAY_TOOLTIP = 'Papich keyboard'
TRAY_ICON = 'icon.png'
KeyBind = 'F9'
StopKeyBind = 'F10'
isWorking = 0
def create_menu_item(menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
menu.Append(item)
return item
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
wx.adv.TaskBarIcon.__init__(self)
self.myapp_frame = frame
self.set_icon(TRAY_ICON)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
def CreatePopupMenu(self):
menu = wx.Menu()
create_menu_item(menu, 'Say Hello', self.on_hello)
menu.AppendSeparator()
create_menu_item(menu, 'Exit', self.on_exit)
create_menu_item(menu, 'Start ('+KeyBind+')', self.on_start)
create_menu_item(menu,'STOP ('+StopKeyBind+')', self.on_stop)
return menu
def set_icon(self, path):
icon = wx.Icon(wx.Bitmap(path))
self.SetIcon(icon, TRAY_TOOLTIP)
def on_left_down(self, event):
print('Tray icon was left-clicked.')
def on_hello(self, event):
print('Hello, world!')
def on_exit(self, event):
self.myapp_frame.Close()
def on_start(self, event):
isWorking = 1
while isWorking == 1:
if kb.is_pressed('f'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_A.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed(','):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_B.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('d'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_V.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('u'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_G.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('l'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_D.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('t'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_E.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('`'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_EO.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed(';'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_J.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('p'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_Z.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('b'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_I.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('q'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_IO.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('r'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_K.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('y'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_N.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('p'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_Z.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('b'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_I.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('q'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_IO.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('r'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_K.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('k'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_L.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('v'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_M.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('y'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_N.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('j'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_O.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('g'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_P.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('h'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_R.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('c'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_S.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('n'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_T.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('e'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_U.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('a'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_F.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('s'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_H.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('w'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_TS.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('x'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_CH.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('i'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_sh.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('o'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_SHZ.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed(']'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_TverdiyZnak.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('s'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_II.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('m'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_MagkiyZnak.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('['):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_H.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('"'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_EIO.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('z'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_IA.wav')
default_speaker.play(samples, samplerate=samplerate)
elif kb.is_pressed('.'):
default_speaker = sc.default_speaker()
samples, samplerate = sf.read('Sounds\Agressive_IU.wav')
default_speaker.play(samples, samplerate=samplerate)
def on_stop(self, event):
isWorking = 0
class My_Application(wx.Frame):
# ----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "", size=(1, 1))
panel = wx.Panel(self)
self.myapp = TaskBarIcon(self)
self.Bind(wx.EVT_CLOSE, self.onClose)
# ----------------------------------------------------------------------
def onClose(self, evt):
"""
Destroy the taskbar icon and the frame
"""
self.myapp.RemoveIcon()
self.myapp.Destroy()
self.Destroy()
if __name__ == "__main__":
MyApp = wx.App()
My_Application()
MyApp.MainLoop()
main_loop = asyncio.get_event_loop()
main_loop.run_until_complete(on_exit())