Python Forum
Disable Enter Key PyQt5 Wizard
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Disable Enter Key PyQt5 Wizard
#1
I am making a Wizard in QWizard

I have QLineEdit and QPushButton

    
    # Enter token
    self.enter_token_box = QLineEdit()

    # Enter token button
    self.btn = QPushButton('OK')

    # connect button to function, checks the token..
    self.btn.clicked.connect(self._EnterToken)
I have put in this line which accepts an enter key press and runs the function the same as clicking the "OK" button.

    
    # Enter key press connection
    self.enter_token_box.returnPressed.connect(self._EnterToken)
The problem is that it will trigger BOTH the OK button AND the Next button of the wizard.



Full MVCE:

    
    import sys
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *


    class Wizard(QWizard):
        def __init__(self, parent=None):
            super(Wizard, self).__init__(parent)

            self.addPage(EnterToken(self)) 
            self.addPage(ProcessData(self))

    class EnterToken(QWizardPage):
        def __init__(self, parent=None):
            super(EnterToken, self).__init__(parent)

            self.setTitle("Enter your token here")
            self.setSubTitle(" ")           

            # Enter Token Widgets
            self.label = QLabel()
            self.enter_token_box = QLineEdit()        

            self.btn = QPushButton('OK')
            
            # layout options
            layout = QVBoxLayout()
            layout.addWidget(self.label)        
            self.label.setText("Enter Your 12 Digit Code.")
            layout.addWidget(self.enter_token_box)
            layout.addWidget(self.btn)

            # Enter Key TRigger
            self.enter_token_box.returnPressed.connect(self._EnterToken)

            self.btn.clicked.connect(self._EnterToken)
            
            self.setLayout(layout)        
            

        def _EnterToken(self):
            """ Method for processing user input after the button is pressed"""

            QMessageBox.about(self, "I want only this!!", "I want only you and not the next page!!")


    class ProcessData(QWizardPage):
        """ Sensor Code Entry """
        def __init__(self, parent=None):
            super(ProcessData, self).__init__(parent)        
        
            # num of logs combo box
            self.num_logs_combo = QComboBox(self)

            # ~buttons
            self.btn = QPushButton('OK')

            layout = QVBoxLayout()
            layout.addWidget(self.num_logs_combo)
            layout.addWidget(self.btn)  
            self.setLayout(layout)    

    if __name__ == '__main__':
        app = QApplication(sys.argv)
        wizard = Wizard()
        wizard.show()
        sys.exit(app.exec_())
If you run the code above and click ok, you will remain on the page. Same thing happens if you have anything selected other than the QLineEdit box.

If you are in the QLineEdit box and you press Enter, you will be taken to the next page as well as displaying the messagebox.

How can I stop the Enter Key from being linked to the Next button.

How can I access and override attributes for the BACK, NEXT and FINISH buttons in QWizard?
Reply
#2
For anyone interested. The problem was solved like so..

To access the buttons you must use the button() method and pass the QWizard::WizardButton, in your case you must disable the default of the QPushButton.

    class Wizard(QWizard):
        def __init__(self, parent=None):
            super(Wizard, self).__init__(parent)
    
            self.addPage(EnterToken(self)) 
            self.addPage(ProcessData(self))
    
        def showEvent(self, event):
            self.button(QWizard.NextButton).setDefault(False)
            super(Wizard, self).showEvent(event)
To disable multiple buttons..

    class Wizard(QWizard):
        def __init__(self, parent=None):
            super(Wizard, self).__init__(parent)
    
            self.addPage(EnterToken(self)) 
            self.addPage(ProcessData(self))
    
            self.buttons = [self.button(t) for t in (QWizard.NextButton, QWizard.FinishButton)]
    
            for btn in self.buttons:
                btn.installEventFilter(self)
    
        def eventFilter(self, obj, event):
            if obj in self.buttons and event.type() == QEvent.Show:
                obj.setDefault(False)
            return super(Wizard, self).eventFilter(obj, event)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] binding versus disable DPaul 6 6,601 May-05-2021, 05:17 PM
Last Post: DPaul
  How to disable custom button Sancho_Pansa 7 3,419 Dec-04-2020, 02:21 PM
Last Post: buran
  How to disable focus on Frame in Tkinter? szafranji 1 2,973 May-13-2020, 10:45 PM
Last Post: DT2000
  Disable entry field and still see value scratchmyhead 5 4,972 May-11-2020, 08:09 PM
Last Post: menator01
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 2,642 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,785 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [PyQt] enter from py exported by pyqt5 designer to another py file royer14 0 2,198 Jun-29-2018, 01:45 AM
Last Post: royer14
  [Tkinter] Disable anti aliasing for text AnonymousNobody 3 6,788 Aug-11-2017, 07:54 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020