Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想在PyQt4中的代码中使用键盘快捷键_Python_Python 2.7_Pyqt_Pyqt4_Keyboard Shortcuts - Fatal编程技术网

Python 我想在PyQt4中的代码中使用键盘快捷键

Python 我想在PyQt4中的代码中使用键盘快捷键,python,python-2.7,pyqt,pyqt4,keyboard-shortcuts,Python,Python 2.7,Pyqt,Pyqt4,Keyboard Shortcuts,我正在开发一个p-room管理程序。当用户按下“ESC”键时,“对话框”终止。我想阻止这一切。所以,我想在“第一个代码”中使用“第二个代码” from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplicatio

我正在开发一个p-room管理程序。当用户按下“ESC”键时,“对话框”终止。我想阻止这一切。所以,我想在“第一个代码”中使用“第二个代码”

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        self.Dialog = Dialog
        self.Dialog.setObjectName(_fromUtf8("self.Dialog"))
        self.Dialog.resize(190, 98)
        self.pushButton = QtGui.QPushButton(self.Dialog)
        self.pushButton.setGeometry(QtCore.QRect(0, 0, 191, 101))
        font = QtGui.QFont()
        font.setPointSize(13)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.retranslateUi(self.Dialog)
        QtCore.QMetaObject.connectSlotsByName(self.Dialog)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL('released()'), self.Dialog.close) # <- put signal to close when clicked.

    def retranslateUi(self, Dialog):
        self.Dialog.setWindowTitle(_translate("self.Dialog", "self.Dialog", None))
        self.pushButton.setText(_translate("self.Dialog", "hi", None))

class QCustomDialog (QtGui.QDialog): # <- Implement your own
    def closeEvent(self, event):
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes | 
            QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QCustomDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

使用键盘快捷键的简单方法是使用
qkeystequence
中的键序列来使用
QShortcut

class MainWindow (QtGui.QMainWindow): 
    def __init__ (self, parent = None): 
        QtGui.QMainWindow.__init__(self, parent)
        .
        .
        .
        self.myQCustomDialog = QCustomDialog() # <- From code 1
        ui = Ui_Dialog()                       # <- From code 1
        ui.setupUi(self.myQCustomDialog)       # <- From code 1
        self.setCentralWidget(self.myQCustomDialog) # <- Set to this central widget
        .
        .
        .
        self.connect(QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Escape), self), QtCore.SIGNAL('activated()'), self.down)

def down(self): 
    print 'DOWN!!!'
    # Or put code to implement from code 1

非常感谢。但我想在“第一个代码”中使用“第二个代码”再进行一次分析..哼????是否要将“QtGui.QMainWindow”放入“QtGui.QDialog”中?这是不可能的。@KitsuneMeyoko顺便说一句。真的不可能将qmain窗口放入QDialog吗?我认为它应该工作,只是表现得有点不同。实际上可以,但它不建议“QMainWindow”不在主窗口小部件中使用QMainWindow类具有主窗口管理。因此,我不建议这样做。在
QKeySequence
类参考链接的末尾缺少一个“l”。你应该更具体一些。在另一个代码中使用一个代码是非常模糊的,不能告诉我们您真正想要什么。
class MainWindow (QtGui.QMainWindow): 
    def __init__ (self, parent = None): 
        QtGui.QMainWindow.__init__(self, parent)
        .
        .
        .
        self.myQCustomDialog = QCustomDialog() # <- From code 1
        ui = Ui_Dialog()                       # <- From code 1
        ui.setupUi(self.myQCustomDialog)       # <- From code 1
        self.setCentralWidget(self.myQCustomDialog) # <- Set to this central widget
        .
        .
        .
        self.connect(QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Escape), self), QtCore.SIGNAL('activated()'), self.down)

def down(self): 
    print 'DOWN!!!'
    # Or put code to implement from code 1
class Ui_Dialog(object):
    def setupUi(self, Dialog):
        .
        .
        .
        QtCore.QObject.connect(QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Escape), self.Dialog), QtCore.SIGNAL('activated()'), self.Dialog.close)