私はPythonの初心者です。私は "Qtデザイナ"を使って簡単な電卓アプリを設計し、 "pyuic5"を使って "ui"ファイルを "py"に変換しました。PyQt5/Python 3+でkeyPressEvent()メソッドが動作しない
しかし、私がkeyPressEvent()メソッドを追加したとき、メソッドは機能しませんでした。そして私は理由を知らない。私はこの問題を克服するためにsetFocus()メソッドを試しましたが失敗しました。
誰でも教えてください。
以下の私のコードを確認してください:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setMinimumSize(QtCore.QSize(330, 280))
MainWindow.setMaximumSize(QtCore.QSize(330, 280))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.lineEdit_display = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_display.setGeometry(QtCore.QRect(10, 0, 311, 41))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.lineEdit_display.setFont(font)
self.lineEdit_display.setMaxLength(15)
self.lineEdit_display.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lineEdit_display.setReadOnly(True)
self.pButton_0 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_0.setGeometry(QtCore.QRect(10, 200, 51, 41))
self.pButton_0.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.pButton_00 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_00.setGeometry(QtCore.QRect(70, 200, 51, 41))
self.pButton_dot = QtWidgets.QPushButton(self.centralwidget)
self.pButton_dot.setGeometry(QtCore.QRect(130, 200, 51, 41))
self.pButton_1 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_1.setGeometry(QtCore.QRect(10, 150, 51, 41))
self.pButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_2.setGeometry(QtCore.QRect(70, 150, 51, 41))
self.pButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_3.setGeometry(QtCore.QRect(130, 150, 51, 41))
self.pButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_4.setGeometry(QtCore.QRect(10, 100, 51, 41))
self.pButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_5.setGeometry(QtCore.QRect(70, 100, 51, 41))
self.pButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_6.setGeometry(QtCore.QRect(130, 100, 51, 41))
self.pButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_7.setGeometry(QtCore.QRect(10, 50, 51, 41))
self.pButton_8 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_8.setGeometry(QtCore.QRect(70, 50, 51, 41))
self.pButton_9 = QtWidgets.QPushButton(self.centralwidget)
self.pButton_9.setGeometry(QtCore.QRect(130, 50, 51, 41))
self.pButton_add = QtWidgets.QPushButton(self.centralwidget)
self.pButton_add.setGeometry(QtCore.QRect(210, 50, 51, 41))
self.pButton_subtract = QtWidgets.QPushButton(self.centralwidget)
self.pButton_subtract.setGeometry(QtCore.QRect(210, 100, 51, 41))
self.pButton_multiplication = QtWidgets.QPushButton(self.centralwidget)
self.pButton_multiplication.setGeometry(QtCore.QRect(210, 150, 51, 41))
self.pButton_divide = QtWidgets.QPushButton(self.centralwidget)
self.pButton_divide.setGeometry(QtCore.QRect(210, 200, 51, 41))
self.pButton_equal = QtWidgets.QPushButton(self.centralwidget)
self.pButton_equal.setGeometry(QtCore.QRect(270, 150, 51, 91))
self.pButton_ac = QtWidgets.QPushButton(self.centralwidget)
self.pButton_ac.setGeometry(QtCore.QRect(270, 50, 51, 91))
font = QtGui.QFont()
font.setPointSize(14)
self.pButton_ac.setFont(font)
self.pButton_ac.setDefault(True)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 330, 27))
self.menuMain = QtWidgets.QMenu(self.menubar)
MainWindow.setMenuBar(self.menubar)
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionAbout = QtWidgets.QAction(MainWindow)
self.menuMain.addAction(self.actionExit)
self.menuMain.addAction(self.actionAbout)
self.menubar.addAction(self.menuMain.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Simple Calculator"))
MainWindow.setToolTip(_translate("MainWindow", "Simple Calculator"))
self.pButton_0.setToolTip(_translate("MainWindow", "0"))
self.pButton_0.setText(_translate("MainWindow", "0"))
self.pButton_00.setToolTip(_translate("MainWindow", "00"))
self.pButton_00.setText(_translate("MainWindow", "00"))
self.pButton_dot.setToolTip(_translate("MainWindow", "."))
self.pButton_dot.setText(_translate("MainWindow", "."))
self.pButton_1.setToolTip(_translate("MainWindow", "1"))
self.pButton_1.setText(_translate("MainWindow", "1"))
self.pButton_2.setToolTip(_translate("MainWindow", "2"))
self.pButton_2.setText(_translate("MainWindow", "2"))
self.pButton_3.setToolTip(_translate("MainWindow", "3"))
self.pButton_3.setText(_translate("MainWindow", "3"))
self.pButton_4.setToolTip(_translate("MainWindow", "4"))
self.pButton_4.setText(_translate("MainWindow", "4"))
self.pButton_5.setToolTip(_translate("MainWindow", "5"))
self.pButton_5.setText(_translate("MainWindow", "5"))
self.pButton_6.setToolTip(_translate("MainWindow", "6"))
self.pButton_6.setText(_translate("MainWindow", "6"))
self.pButton_7.setToolTip(_translate("MainWindow", "7"))
self.pButton_7.setText(_translate("MainWindow", "7"))
self.pButton_8.setToolTip(_translate("MainWindow", "8"))
self.pButton_8.setText(_translate("MainWindow", "8"))
self.pButton_9.setToolTip(_translate("MainWindow", "9"))
self.pButton_9.setText(_translate("MainWindow", "9"))
self.pButton_add.setToolTip(_translate("MainWindow", "+"))
self.pButton_add.setText(_translate("MainWindow", "+"))
self.pButton_subtract.setToolTip(_translate("MainWindow", "-"))
self.pButton_subtract.setText(_translate("MainWindow", "-"))
self.pButton_multiplication.setToolTip(_translate("MainWindow", "\N{MULTIPLICATION SIGN}"))
self.pButton_multiplication.setText(_translate("MainWindow", "\N{MULTIPLICATION SIGN}"))
self.pButton_divide.setToolTip(_translate("MainWindow", "\N{DIVISION SIGN}"))
self.pButton_divide.setText(_translate("MainWindow", "\N{DIVISION SIGN}"))
self.pButton_equal.setToolTip(_translate("MainWindow", "="))
self.pButton_equal.setText(_translate("MainWindow", "="))
self.pButton_ac.setToolTip(_translate("MainWindow", "AC"))
self.pButton_ac.setText(_translate("MainWindow", "AC"))
self.lineEdit_display.setToolTip(_translate("MainWindow", "Display"))
self.lineEdit_display.setText(_translate("MainWindow", "0"))
self.menuMain.setTitle(_translate("MainWindow", "Mai&n"))
self.actionExit.setText(_translate("MainWindow", "&Exit"))
self.actionAbout.setText(_translate("MainWindow", "&About"))
def keyPressEvent(self, e):
print(e.key())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
MainWindow.setFocus() #TRYING TO SET FOCUS FOR keyPressEvent
sys.exit(app.exec_())
ここでは、keyPressEventの中で必要に応じてロジックを扱うだけです。たとえば "if e.key()== Qt.Key_1"を比較すると.... – yurisnm
ありがとうございました。マウスイベントを含むコード全体でうまく動作します。今、私はマウスイベントとキーボードイベントの両方を使うことができます! :)) – smearumi