qt - keyPressEvent() method doesn't work for PyQt5 / Python 3+ -


i'm newbie python. i've designed simple calculator app using "qt designer" , convert "ui" file "py" using "pyuic5".

but when i've added keypressevent() method, method didn't work. , don't know why. i've tried setfocus() method overcome issue failed.

can me please?

please check code below:

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 set focus keypressevent      sys.exit(app.exec_()) 

sir, couldn't find error in syntax, edited code keep similar yours possible.

here code working modifications, see if work you.

from pyqt5 import qtcore, qtgui, qtwidgets import sys  pyqt5.qtwidgets import qaction pyqt5.qtwidgets import qlineedit pyqt5.qtwidgets import qmainwindow pyqt5.qtwidgets import qmenu pyqt5.qtwidgets import qmenubar pyqt5.qtwidgets import qpushbutton 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_()) 

hope works ^^


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -