Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TAB key is not working in PYQT5

TAB key is not working in PYQT5

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 3.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • monamourM Offline
    monamourM Offline
    monamour
    wrote on last edited by
    #1

    Dear All.

     Once I press the "TAB KEY" from the keyboard, should move to the next field and execute function or just print 'xxx'.
    

    The code:

    def keyPressEvent(self, event):
            key = event.key()
            if key == QtCore.Qt.Key_Tab:
                 #self.ID()
                 print('xxx')
    

    But is not working at all.

    I am using pyqt5

    Many thanks,

    jsulmJ 1 Reply Last reply
    0
    • monamourM monamour

      Dear All.

       Once I press the "TAB KEY" from the keyboard, should move to the next field and execute function or just print 'xxx'.
      

      The code:

      def keyPressEvent(self, event):
              key = event.key()
              if key == QtCore.Qt.Key_Tab:
                   #self.ID()
                   print('xxx')
      

      But is not working at all.

      I am using pyqt5

      Many thanks,

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @monamour said in TAB key is not working in PYQT5:

      keyPressEvent

      Did you make sure it is called?
      Also: where (in which class/widget) did you actually define it?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • monamourM Offline
        monamourM Offline
        monamour
        wrote on last edited by
        #3

        Hi Mr. jsulm, this is my whole code,

        
        from PyQt5 import QtCore, QtGui, QtWidgets
        
        
        
        class Ui_MainWindow(object):
            def setupUi(self, MainWindow):
                MainWindow.setObjectName("MainWindow")
                MainWindow.resize(800, 600)
                self.centralwidget = QtWidgets.QWidget(MainWindow)
                self.centralwidget.setObjectName("centralwidget")
                self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
                self.lineEdit.setGeometry(QtCore.QRect(282, 130, 181, 41))
                self.lineEdit.setObjectName("lineEdit")
                self.label = QtWidgets.QLabel(self.centralwidget)
                self.label.setGeometry(QtCore.QRect(216, 140, 51, 20))
                font = QtGui.QFont()
                font.setPointSize(15)
                self.label.setFont(font)
                self.label.setObjectName("label")
                self.label_2 = QtWidgets.QLabel(self.centralwidget)
                self.label_2.setGeometry(QtCore.QRect(210, 210, 51, 41))
                font = QtGui.QFont()
                font.setPointSize(15)
                self.label_2.setFont(font)
                self.label_2.setObjectName("label_2")
                self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
                self.textBrowser.setGeometry(QtCore.QRect(280, 210, 91, 41))
                sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
                sizePolicy.setHorizontalStretch(0)
                sizePolicy.setVerticalStretch(0)
                sizePolicy.setHeightForWidth(self.textBrowser.sizePolicy().hasHeightForWidth())
                self.textBrowser.setSizePolicy(sizePolicy)
                self.textBrowser.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
                self.textBrowser.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
                self.textBrowser.setTabChangesFocus(False)
                self.textBrowser.setObjectName("textBrowser")
                MainWindow.setCentralWidget(self.centralwidget)
                self.menubar = QtWidgets.QMenuBar(MainWindow)
                self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
                self.menubar.setObjectName("menubar")
                MainWindow.setMenuBar(self.menubar)
                self.statusbar = QtWidgets.QStatusBar(MainWindow)
                self.statusbar.setObjectName("statusbar")
                MainWindow.setStatusBar(self.statusbar)
                #self.Handel_Buttons()
                self.retranslateUi(MainWindow)
                QtCore.QMetaObject.connectSlotsByName(MainWindow)
        
            def keyPressEvent(self, event):
                key = event.key()
                if key == QtCore.Qt.Key_Tab:
                     #self.ID()
                     print('xxx')
        
        
            def retranslateUi(self, MainWindow):
                _translate = QtCore.QCoreApplication.translate
                MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
                self.label.setText(_translate("MainWindow", "ID"))
                self.label_2.setText(_translate("MainWindow", "D.O.B"))
        
          
        
        if __name__ == "__main__":
            import sys
            app = QtWidgets.QApplication(sys.argv)
            MainWindow = QtWidgets.QMainWindow()
            ui = Ui_MainWindow()
            ui.setupUi(MainWindow)
            MainWindow.show()
            sys.exit(app.exec_())
        
        
        jsulmJ 1 Reply Last reply
        0
        • monamourM monamour

          Hi Mr. jsulm, this is my whole code,

          
          from PyQt5 import QtCore, QtGui, QtWidgets
          
          
          
          class Ui_MainWindow(object):
              def setupUi(self, MainWindow):
                  MainWindow.setObjectName("MainWindow")
                  MainWindow.resize(800, 600)
                  self.centralwidget = QtWidgets.QWidget(MainWindow)
                  self.centralwidget.setObjectName("centralwidget")
                  self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
                  self.lineEdit.setGeometry(QtCore.QRect(282, 130, 181, 41))
                  self.lineEdit.setObjectName("lineEdit")
                  self.label = QtWidgets.QLabel(self.centralwidget)
                  self.label.setGeometry(QtCore.QRect(216, 140, 51, 20))
                  font = QtGui.QFont()
                  font.setPointSize(15)
                  self.label.setFont(font)
                  self.label.setObjectName("label")
                  self.label_2 = QtWidgets.QLabel(self.centralwidget)
                  self.label_2.setGeometry(QtCore.QRect(210, 210, 51, 41))
                  font = QtGui.QFont()
                  font.setPointSize(15)
                  self.label_2.setFont(font)
                  self.label_2.setObjectName("label_2")
                  self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
                  self.textBrowser.setGeometry(QtCore.QRect(280, 210, 91, 41))
                  sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
                  sizePolicy.setHorizontalStretch(0)
                  sizePolicy.setVerticalStretch(0)
                  sizePolicy.setHeightForWidth(self.textBrowser.sizePolicy().hasHeightForWidth())
                  self.textBrowser.setSizePolicy(sizePolicy)
                  self.textBrowser.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
                  self.textBrowser.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
                  self.textBrowser.setTabChangesFocus(False)
                  self.textBrowser.setObjectName("textBrowser")
                  MainWindow.setCentralWidget(self.centralwidget)
                  self.menubar = QtWidgets.QMenuBar(MainWindow)
                  self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
                  self.menubar.setObjectName("menubar")
                  MainWindow.setMenuBar(self.menubar)
                  self.statusbar = QtWidgets.QStatusBar(MainWindow)
                  self.statusbar.setObjectName("statusbar")
                  MainWindow.setStatusBar(self.statusbar)
                  #self.Handel_Buttons()
                  self.retranslateUi(MainWindow)
                  QtCore.QMetaObject.connectSlotsByName(MainWindow)
          
              def keyPressEvent(self, event):
                  key = event.key()
                  if key == QtCore.Qt.Key_Tab:
                       #self.ID()
                       print('xxx')
          
          
              def retranslateUi(self, MainWindow):
                  _translate = QtCore.QCoreApplication.translate
                  MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
                  self.label.setText(_translate("MainWindow", "ID"))
                  self.label_2.setText(_translate("MainWindow", "D.O.B"))
          
            
          
          if __name__ == "__main__":
              import sys
              app = QtWidgets.QApplication(sys.argv)
              MainWindow = QtWidgets.QMainWindow()
              ui = Ui_MainWindow()
              ui.setupUi(MainWindow)
              MainWindow.show()
              sys.exit(app.exec_())
          
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @monamour Did you make sure keyPressEvent called?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          monamourM 1 Reply Last reply
          0
          • jsulmJ jsulm

            @monamour Did you make sure keyPressEvent called?

            monamourM Offline
            monamourM Offline
            monamour
            wrote on last edited by
            #5

            @jsulm I don't know if I did call it or not I just run the program then press 'TAB' from the keyboard.

            jsulmJ 1 Reply Last reply
            0
            • monamourM monamour

              @jsulm I don't know if I did call it or not I just run the program then press 'TAB' from the keyboard.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #6

              @monamour It's not you who calls it.
              Simply do

              def keyPressEvent(self, event):
                  print("keyPressEvent called")
                      key = event.key()
                      if key == QtCore.Qt.Key_Tab:
                           #self.ID()
                           print('xxx')
              

              and see whether "keyPressEvent called" is printed...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              monamourM 2 Replies Last reply
              1
              • jsulmJ jsulm

                @monamour It's not you who calls it.
                Simply do

                def keyPressEvent(self, event):
                    print("keyPressEvent called")
                        key = event.key()
                        if key == QtCore.Qt.Key_Tab:
                             #self.ID()
                             print('xxx')
                

                and see whether "keyPressEvent called" is printed...

                monamourM Offline
                monamourM Offline
                monamour
                wrote on last edited by
                #7

                @jsulm said in TAB key is not working in PYQT5:

                print("keyPressEvent called")

                It did not.

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @monamour It's not you who calls it.
                  Simply do

                  def keyPressEvent(self, event):
                      print("keyPressEvent called")
                          key = event.key()
                          if key == QtCore.Qt.Key_Tab:
                               #self.ID()
                               print('xxx')
                  

                  and see whether "keyPressEvent called" is printed...

                  monamourM Offline
                  monamourM Offline
                  monamour
                  wrote on last edited by monamour
                  #8

                  @jsulm Hi, Now the code has changed to,

                  def keyPressEvent(self):
                          print('HI')
                          if QtCore.Qt.Key_Tab:
                              self.ID()
                  

                  'ID function'

                   def ID(self):
                          number = self.lineEdit.text()
                              digit = int(number[5])
                              digit1 = int(number[6])
                              digit2 = int(number[3])
                              digit3 = int(number[4])
                              digit4 = int(number[1])
                              digit5 = int(number[2])
                  ``
                  self.textBrowser.append ('%d%d/%d%d/19%d%d' % (digit, digit1,digit2,digit3,digit4,digit5)) 
                  

                  it's printed the statement 'HI', however, error when execute the function 'ID' ,

                   digit = int(number[5])
                  IndexError: string index out of range
                  

                  Thanks,

                  1 Reply Last reply
                  0
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #9

                    The key event listener shouldn't go under the UI class. It needs to be under the QWidget subclass being displayed...in your case QMainWindow. the UI class doesn't inherit from a base that supplies that method, so it will never be called.

                    If you meet the AI on the road, kill it.

                    monamourM 1 Reply Last reply
                    0
                    • Kent-DorfmanK Kent-Dorfman

                      The key event listener shouldn't go under the UI class. It needs to be under the QWidget subclass being displayed...in your case QMainWindow. the UI class doesn't inherit from a base that supplies that method, so it will never be called.

                      monamourM Offline
                      monamourM Offline
                      monamour
                      wrote on last edited by monamour
                      #10

                      @Kent-Dorfman Hi dear, this the whole code,

                      
                      
                      from PyQt5 import QtCore, QtGui, QtWidgets
                      
                      class Ui_MainWindow(object):
                          def setupUi(self, MainWindow):
                              MainWindow.setObjectName("MainWindow")
                              MainWindow.resize(800, 600)
                              self.centralwidget = QtWidgets.QWidget(MainWindow)
                              self.centralwidget.setObjectName("centralwidget")
                              self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
                              self.lineEdit.setGeometry(QtCore.QRect(282, 130, 181, 41))
                              self.lineEdit.setObjectName("lineEdit")
                              self.label = QtWidgets.QLabel(self.centralwidget)
                              self.label.setGeometry(QtCore.QRect(216, 140, 51, 20))
                              font = QtGui.QFont()
                              font.setPointSize(15)
                              self.label.setFont(font)
                              self.label.setObjectName("label")
                              self.label_2 = QtWidgets.QLabel(self.centralwidget)
                              self.label_2.setGeometry(QtCore.QRect(210, 210, 51, 41))
                              font = QtGui.QFont()
                              font.setPointSize(15)
                              self.label_2.setFont(font)
                              self.label_2.setObjectName("label_2")
                              self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
                              self.textBrowser.setGeometry(QtCore.QRect(280, 210, 91, 41))
                              sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
                              sizePolicy.setHorizontalStretch(0)
                              sizePolicy.setVerticalStretch(0)
                              sizePolicy.setHeightForWidth(self.textBrowser.sizePolicy().hasHeightForWidth())
                              self.textBrowser.setSizePolicy(sizePolicy)
                              self.textBrowser.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
                              self.textBrowser.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
                              self.textBrowser.setTabChangesFocus(False)
                              self.textBrowser.setObjectName("textBrowser")
                              self.pushButton = QtWidgets.QPushButton(self.centralwidget)
                              self.pushButton.setGeometry(QtCore.QRect(290, 290, 75, 23))
                              self.pushButton.setObjectName("pushButton")
                              MainWindow.setCentralWidget(self.centralwidget)
                              self.menubar = QtWidgets.QMenuBar(MainWindow)
                              self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
                              self.menubar.setObjectName("menubar")
                              MainWindow.setMenuBar(self.menubar)
                              self.statusbar = QtWidgets.QStatusBar(MainWindow)
                              self.statusbar.setObjectName("statusbar")
                              MainWindow.setStatusBar(self.statusbar)
                              self.retranslateUi(MainWindow)
                              QtCore.QMetaObject.connectSlotsByName(MainWindow)
                              
                              self.Handel_Buttons()
                              self.keyPressEvent()
                      
                          def retranslateUi(self, MainWindow):
                              _translate = QtCore.QCoreApplication.translate
                              MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
                              self.label.setText(_translate("MainWindow", "ID"))
                              self.label_2.setText(_translate("MainWindow", "D.O.B"))
                              self.pushButton.setText(_translate("MainWindow", "PushButton"))
                      
                          def Handel_Buttons(self):
                              self.pushButton.clicked.connect(self.ID)
                      
                      
                          def keyPressEvent(self):
                              print('HI')
                              if QtCore.Qt.Key_Tab:
                                  self.ID()
                      
                      
                      
                          def ID(self):
                               number = self.lineEdit.text()
                               #number = '279121100762'
                              #print(int(number[5]))
                               digit = int(number[5])
                               digit1 = int(number[6])
                               digit2 = int(number[3])
                               digit3 = int(number[4])
                               digit4 = int(number[1])
                               digit5 = int(number[2])
                              #self.textBrowser.append(str[digit,digit1])
                      
                               self.textBrowser.append ('%d%d/%d%d/19%d%d' % (digit, digit1,digit2,digit3,digit4,digit5))
                      
                      
                      
                      
                      
                      if __name__ == "__main__":
                          import sys
                          app = QtWidgets.QApplication(sys.argv)
                          MainWindow = QtWidgets.QMainWindow()
                          ui = Ui_MainWindow()
                          ui.setupUi(MainWindow)
                          MainWindow.show()
                          sys.exit(app.exec_())
                      
                      
                      

                      0_1554528826850_fff.JPG

                      1 Reply Last reply
                      0
                      • Kent-DorfmanK Offline
                        Kent-DorfmanK Offline
                        Kent-Dorfman
                        wrote on last edited by
                        #11

                        I see that, but it's still wrong...you need to subclass your main window and override the key event handler there.

                        If you meet the AI on the road, kill it.

                        monamourM 1 Reply Last reply
                        0
                        • Kent-DorfmanK Kent-Dorfman

                          I see that, but it's still wrong...you need to subclass your main window and override the key event handler there.

                          monamourM Offline
                          monamourM Offline
                          monamour
                          wrote on last edited by
                          #12

                          @Kent-Dorfman Please check it after it has edited it again. Many thanks,

                          1 Reply Last reply
                          0
                          • Kent-DorfmanK Offline
                            Kent-DorfmanK Offline
                            Kent-Dorfman
                            wrote on last edited by
                            #13

                            based on what you REALLY ARE TRYING TO DO, forget about the tab key and follow the path discussed in <This Link>

                            If you meet the AI on the road, kill it.

                            monamourM 1 Reply Last reply
                            1
                            • Kent-DorfmanK Kent-Dorfman

                              based on what you REALLY ARE TRYING TO DO, forget about the tab key and follow the path discussed in <This Link>

                              monamourM Offline
                              monamourM Offline
                              monamour
                              wrote on last edited by
                              #14

                              @Kent-Dorfman Many thanks,

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved