keyboard.on press key () stops calling the function after compilation в.ехе
Keyboard. on_press_key('f7', TranslateAll, suppress=True) when pressed on F7 calls the function TranslateAll()
.
Algorithm of operation TranslateAll():
- The library
pyperclip
pulls text from the clipboard - Library
googletrans
translates text -
pyperclip
inserts the translated text back into the b/o
Everything works fine, however, after compiling to. exe, after ~10 function calls, keyboard.on_press_key()
stops responding to clicks or call a function. And this is not accompanied by any messages in the console. Just press F7 and that's it-no errors.
!!! The problem appears only when working with the translator googletrans
, if you drop it and leave only the clipboard, the problem will go away!
I tried to specify the key code, instead of 'f7' - the problem remained.
I also tried re-assigning F7 to keyboard.hook_key()
on error (wrapping the algorithm in try-except), but it also did nothing.
Code: Here everything works as it should, but if you compile (pyinstaller PythonCode.py), then you can see the problem I described.
from googletrans import Translator
import keyboard
import googletrans
import pyperclip
count = 0 #счетчик срабатываний функции
def TranslateAll(event):
global count
#основной алгоритм
translator = Translator()
data = pyperclip.paste()
result = translator.translate(data, dest='ru')
pyperclip.copy(result.text)
count += 1
print(f'Срабатывание №{count}\n')
#привязка F7 к функции
keyboard.on_press_key('f7', TranslateAll, suppress=True)
keyboard.wait()
The finished problematic. exe file can also be downloaded from my Google drive: PythonCode.exe
There is also a video with a detailed overview of the problem: video
1 answers
So, after 5 days of searching for a solution to the problem, the answer is found!
The problem was in the function call string keyboard.on_press_key('f7', TranslateAll, suppress=True)
The value of the suppress
parameter should have been changed from True
to False
.
Thank you to everyone who tried to help in any way :)