How do you track the pressure on the keyboard in the background mode and open the PyQT window?
-
I have a method that opens a new window. I tried to call him with the library.
keyboard
♪keyboard.add_hotkey()
but for some reason, the program is flying.If you try to open this window not through the keyboard, it's through.
QPushButton
It's all right.I have the task of opening a subsidiary window of the application with a combination of clavicle, and not in the main window.
import sys import keyboard from PyQt5.QtWidgets import *
class App(QWidget):
def init(self):
super().init()
self.initUI()def initUI(self): self.setWindowTitle('Image') self.setGeometry(50, 50, 640, 480) self.but = QPushButton(self) self.but.setGeometry(0,0, 300,300) self.but.clicked.connect(self.ran) self.App = App2() keyboard.add_hotkey("ctrl", self.ran) def ran(self): self.App.show()
class App2(QWidget):
def init(self):
super().init()
self.initUI()def initUI(self): self.setWindowTitle('Image') self.setGeometry(0, 0, 100, 100)
if name == 'main':
app = QApplication(sys.argv)
w = App()
w.show()
sys.exit(app.exec())
-
The possible solution is to use signals:
import sys import keyboard from PyQt5.Qt import *
class KeyBoardManager(QObject):
keySignal = pyqtSignal(str)def start(self): keyboard.add_hotkey( "Ctrl + A", lambda: self.keySignal.emit("Ctrl+A"), suppress=True) keyboard.add_hotkey( "Q", lambda: self.keySignal.emit("Q"), suppress=True)
class App2(QWidget):
def init(self):
super().init()
self.initUI()def initUI(self): self.setWindowTitle('Image') self.setGeometry(410, 580, 100, 100)
class App(QWidget):
def init(self):
super().init()
self.initUI()manager = KeyBoardManager(self) manager.keySignal.connect(self.ran) manager.start() def initUI(self): self.setWindowTitle('Image') self.resize(400, 300) self.but = QPushButton(self) self.but.setFixedSize(100, 100) self.but.clicked.connect(lambda: self.ran('btn')) self.app_2 = App2()
keyboard.add_hotkey("ctrl", self.ran)
def ran(self, value=None): if value == 'btn': print("Вы нажали кнопку") elif value == 'Q': print("QApplication.quit()") QApplication.quit() elif value == 'Ctrl+A': print("Вы нажали 'Ctrl+A'") self.app_2.show() def closeEvent(self, event): self.app_2.close()
if name == 'main':
app = QApplication(sys.argv)
w = App()
w.show()
sys.exit(app.exec())