The problem only appears after compiling to .exe: in line keyboard.hook_key ('f7', TranslateAll, suppress = True) when pressing F7 calls the function TranslateAll. Function algorithm:
- Library
pyperclippulls text from the clipboard - Library
googletranstranslates text pyperclipinserts the translated text into clipboard
Everything works fine, however, after compiling to .exe, after 10 function calls, keyboard.hook_key() stops responding.
I've tried reassigning F7 in keyboard.hook_key () on error, but that didn't work either.
What could be the problem?
The problematic part of the code: (You can try running it to see it work and then using pyinstaller "NameOfCode.py" to see the problem I described)
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QSize
from googletrans import Translator
import keyboard
import googletrans
import pyperclip
count = 0 #function operation counter
TranslateAll_count = 0 #variable, for single
#triggering a function when a button is pressed
def TranslateAll(event):
global TranslateAll_count
global count
TranslateAll_count += 1
if TranslateAll_count != 2:
#main algorithm
translator = Translator()
data = pyperclip.paste()
result = translator.translate(data, dest='ru')
pyperclip.copy(result.text)
TranslateAll_count = 1
count += 1
print(f'Actuation №{count}\n')
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setFixedSize(QSize(480, 180))
self.setWindowTitle("test")
#binding F7 to a function TranslateAll
keyboard.hook_key('f7', TranslateAll, suppress=True)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec())
Output when running a .py file:
Actuation №1
Actuation №2
Actuation №3
Actuation №4
#until the user finishes work
Output when running .exe file:
#7 Actuations
Actuation №8
#the function stops being called
The problem was in the function call line
keyboard.hook_key('f7', TranslateAll, suppress=True)To get rid of the problem, it was only necessary to change the value of the
suppressparameter fromTruetoFalse