如何将键分配给pyqt5中的变量?
How to assign Keys to Variables in pyqt5?
如何在pyqt5中为变量赋值?
def keyPressEvent(self, event):
var = "Qt.Key_F"
if event.key() == var:
print("you press key F")
Edit-:图片更多更清晰,添加如下脚本。在我的代码中,第一个和第二个事件根据需要产生结果并打印结果但是第三个事件,即来自字典(Alt+Z)的assigned/got,是没有 produce/prints 任何结果。如何解决?
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()
var = "Qt.Key_F"
这为变量分配了一个字符串。该字符串与 Qt 或“键”无关。我想你想要的是
var = Qt.Key_F
但这并不是真正必要的,因为 Key_F
已经是一个变量。可以直接使用:
if event.key() == Qt.Key_F:
我认为这是 XY problem。您的目标是检测按下了哪个字母并在它与变量的值匹配时执行某些操作,如果匹配则解决方案是使用 event.text():
def keyPressEvent(self, event):
v = "F"
if event.text() == var:
# OR
# if event.text().upper() == var:
print("you press key F")
你正在使你的工作复杂化,Qt 已经有 类 允许你通过 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()
如果你想有一个更友好的环境来设置 QKeySequence 那么你可以使用 QKeySequenceEdit
.
如何在pyqt5中为变量赋值?
def keyPressEvent(self, event):
var = "Qt.Key_F"
if event.key() == var:
print("you press key F")
Edit-:图片更多更清晰,添加如下脚本。在我的代码中,第一个和第二个事件根据需要产生结果并打印结果但是第三个事件,即来自字典(Alt+Z)的assigned/got,是没有 produce/prints 任何结果。如何解决?
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()
var = "Qt.Key_F"
这为变量分配了一个字符串。该字符串与 Qt 或“键”无关。我想你想要的是
var = Qt.Key_F
但这并不是真正必要的,因为 Key_F
已经是一个变量。可以直接使用:
if event.key() == Qt.Key_F:
我认为这是 XY problem。您的目标是检测按下了哪个字母并在它与变量的值匹配时执行某些操作,如果匹配则解决方案是使用 event.text():
def keyPressEvent(self, event):
v = "F"
if event.text() == var:
# OR
# if event.text().upper() == var:
print("you press key F")
你正在使你的工作复杂化,Qt 已经有 类 允许你通过 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()
如果你想有一个更友好的环境来设置 QKeySequence 那么你可以使用 QKeySequenceEdit
.