B
B
Bogdan Karpov2018-05-17 13:28:41
Python
Bogdan Karpov, 2018-05-17 13:28:41

How to run a method in PyQt when a keyboard key is pressed?

There is a class, how to determine keystrokes in it, let's say (Ё) on the keyboard and run some method.
Here is part of the class code:

import sys
from wikides import *
from wikicore import *
from PyQt5 import QtCore, QtGui, QtWidgets
class MyWin(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.path = os.path.dirname(__file__)
        self.pathTemp = self.path +'/temp'
        # Клик по кнопке. 
        self.ui.pushButton.clicked.connect(self.WikiGo)

    def WikiGo(self):
        cmd = recToText()
        self.showText(cmd)
        # ....... и т. д.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andy_U, 2018-05-17
@phpkoder

Ha, just went through this rake:
Add a method:

def keyPressEvent(self, e):

            if e.key() in [QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return]:
                self.WikiGo()
            else:
                super(MyWindow, self).keyPressEvent(e)

But my class is inherited from Ui_MainWindow, so there are options ...

T
TomasHuk, 2018-05-17
@TomasHuk

Try adding a method to the MyWin class:

def keyPressEvent(self, e):
    if e.key() == QtCore.Qt.Key_Q:
        self.run_me()

Written for the Q key. Look for Y (~), although I'm not sure.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question