Issue
How to assign Keys to Variables in pyqt5?
def keyPressEvent(self, event):
var = "Qt.Key_F"
if event.key() == var:
print("you press key F")
Edit-: For more and clear pictures, add the following script. In my code, The first and second event produce the result as desired and prints the result But the third event, which is assigned/got it from dictionery(Alt+Z), is not produce/prints any results . How to resolve it ?
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
dict_scut = {
"eng":{ "A": "Qt.Key_A", "B": "Qt.Key_B","C": "Qt.Key_C","D": "Qt.Key_D","E": "Qt.Key_E","F": "Qt.Key_F",
"G": "Qt.Key_G", "H": "Qt.Key_H","I": "Qt.Key_I","J": "Qt.Key_J","K": "Qt.Key_K","L": "Qt.Key_L",
"M": "Qt.Key_M", "N": "Qt.Key_N","O": "Qt.Key_O","P": "Qt.Key_P","Q": "Qt.Key_Q","R": "Qt.Key_R",
"S": "Qt.Key_S", "T": "Qt.Key_T","U": "Qt.Key_U","V": "Qt.Key_V","W": "Qt.Key_W","X": "Qt.Key_X",
"Y": "Qt.Key_Y", "Z": "Qt.Key_Z","None":""}
}
dict_modi = {"None":" ","alt":"Qt.AltModifier","ctrl":"Qt.ControlModifier","shift":"Qt.ShiftModifier",
"altctrl":"Qt.AltModifier|Qt.ControlModifier","altshift":"Qt.AltModifier|Qt.ShiftModifier",
"ctrlshift":"Qt.ControlModifier|Qt.ShiftModifier",
"altctrlshift":"Qt.AltModifier|Qt.ControlModifier|Qt.ShiftModifier"}
class Dynamic_shortcut(QWidget):
def __init__(self):
super(). __init__()
self.setWindowTitle("Dynamic Widget")
self.var_modi_1 = Qt.AltModifier
self.var_scut_1 = Qt.Key_A
user1_modi = "alt"
user1_scut = "Z"
self.var_modi_2 = dict_modi.get(user1_modi)
self.var_scut_2 = dict_scut['eng'].get(user1_scut)
def keyPressEvent(self, event):
if event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_Q:
print("Pressed key : Alt + Q")
if event.modifiers() == self.var_modi_1 and event.key() == self.var_scut_1:
print("Pressed key : Alt + A")
if event.modifiers() == self.var_modi_2 and event.key() == self.var_scut_2:
print("pressed key : Alt + Z")
def main():
app = QApplication(sys.argv)
ex = Dynamic_shortcut()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Solution
I think this is an XY problem. Your goal is to detect which letter is pressed and perform some action if it matches the value of a variable, if so the solution is to use event.text():
def keyPressEvent(self, event):
v = "F"
if event.text() == var:
# OR
# if event.text().upper() == var:
print("you press key F")
You are complicating your work, Qt already has classes that allow you to handle this logic in a simple way through QKeySequence
and QShortcut
:
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QApplication, QShortcut, QWidget
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
keysequence1 = QKeySequence("ALT+A")
# OR
# keysequence1 = QKeySequence(Qt.ALT + Qt.Key_A)
shorcut1 = QShortcut(keysequence1, self, activated=self.handle_activated)
keysequence2 = QKeySequence("alt+z")
# OR
# keysequence1 = QKeySequence(Qt.ALT + Qt.Key_Z)
shorcut2 = QShortcut(keysequence2, self, activated=self.handle_activated)
def handle_activated(self):
self.do_task()
def do_task(self):
print("Qt is Awesome!!!")
def main():
app = QApplication([])
w = Widget()
w.show()
app.exec_()
if __name__ == "__main__":
main()
If you want to have a more user friendly environment for setting a QKeySequence then you can use QKeySequenceEdit
.
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.