gpt4 book ai didi

python - keyPressEvent() 方法不适用于 PyQt5/Python 3+

转载 作者:行者123 更新时间:2023-12-01 03:35:17 25 4
gpt4 key购买 nike

我是Python新手。我使用“Qt Designer”设计了一个简单的计算器应用程序,然后使用“pyuic5”将“ui”文件转换为“py”。

但是当我添加 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_())

最佳答案

先生,我无法具体找到您的语法错误是什么,因此我编辑了您的代码以尽可能与您的代码保持相似。

这是您的代码,经过一些修改,看看它是否适合您。

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

from PyQt5.QtWidgets import QAction
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMenu
from PyQt5.QtWidgets import QMenuBar
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QWidget


class Ui_MainWindow(QMainWindow):

def __init__(self):
super(Ui_MainWindow, self).__init__()
# self.setupUi()

def setupUi(self):
self.setMinimumSize(QtCore.QSize(330, 280))
self.setMaximumSize(QtCore.QSize(330, 280))

self.centralwidget = QWidget()


self.lineEdit_display = 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 = 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 = QPushButton(self.centralwidget)
self.pButton_00.setGeometry(QtCore.QRect(70, 200, 51, 41))

self.pButton_dot = QPushButton(self.centralwidget)
self.pButton_dot.setGeometry(QtCore.QRect(130, 200, 51, 41))

self.pButton_1 = QPushButton(self.centralwidget)
self.pButton_1.setGeometry(QtCore.QRect(10, 150, 51, 41))

self.pButton_2 = QPushButton(self.centralwidget)
self.pButton_2.setGeometry(QtCore.QRect(70, 150, 51, 41))

self.pButton_3 = QPushButton(self.centralwidget)
self.pButton_3.setGeometry(QtCore.QRect(130, 150, 51, 41))

self.pButton_4 = QPushButton(self.centralwidget)
self.pButton_4.setGeometry(QtCore.QRect(10, 100, 51, 41))

self.pButton_5 = QPushButton(self.centralwidget)
self.pButton_5.setGeometry(QtCore.QRect(70, 100, 51, 41))

self.pButton_6 = QPushButton(self.centralwidget)
self.pButton_6.setGeometry(QtCore.QRect(130, 100, 51, 41))

self.pButton_7 = QPushButton(self.centralwidget)
self.pButton_7.setGeometry(QtCore.QRect(10, 50, 51, 41))

self.pButton_8 = QPushButton(self.centralwidget)
self.pButton_8.setGeometry(QtCore.QRect(70, 50, 51, 41))

self.pButton_9 = QPushButton(self.centralwidget)
self.pButton_9.setGeometry(QtCore.QRect(130, 50, 51, 41))

self.pButton_add = QPushButton(self.centralwidget)
self.pButton_add.setGeometry(QtCore.QRect(210, 50, 51, 41))

self.pButton_subtract = QPushButton(self.centralwidget)
self.pButton_subtract.setGeometry(QtCore.QRect(210, 100, 51, 41))

self.pButton_multiplication = QPushButton(self.centralwidget)
self.pButton_multiplication.setGeometry(QtCore.QRect(210, 150, 51, 41))

self.pButton_divide = QPushButton(self.centralwidget)
self.pButton_divide.setGeometry(QtCore.QRect(210, 200, 51, 41))

self.pButton_equal = QPushButton(self.centralwidget)
self.pButton_equal.setGeometry(QtCore.QRect(270, 150, 51, 91))

self.pButton_ac = 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)

self.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(self)
self.menubar.setGeometry(QtCore.QRect(0, 0, 330, 27))
self.menuMain = QMenu(self.menubar)
self.setMenuBar(self.menubar)
self.actionExit = QAction(self)
self.actionAbout = QAction(self)
self.menuMain.addAction(self.actionExit)
self.menuMain.addAction(self.actionAbout)
self.menubar.addAction(self.menuMain.menuAction())

self.retranslateUi()
QtCore.QMetaObject.connectSlotsByName(self)

def retranslateUi(self):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("MainWindow", "Simple Calculator"))
self.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)

ui = Ui_MainWindow()
ui.setupUi()
ui.show()

sys.exit(app.exec_())

希望它有效^^

关于python - keyPressEvent() 方法不适用于 PyQt5/Python 3+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40443523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com