我正在制作一个向导QWizard
我有 QLineEdit
和 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)
我已经放入了接受输入键并运行与单击“确定”按钮相同的功能的行。
# Enter key press connection
self.enter_token_box.returnPressed.connect(self._EnterToken)
问题是它会同时触发OK
按钮和Next
向导的按钮。
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_())
如果您运行上面的代码并单击ok
,您将留在页面上。QLineEdit
如果您选择了除该框之外的任何其他内容,也会发生同样的事情。
如果您在QLineEdit
框中并按Enter
,您将被带到下一页并显示消息框。
如何阻止Enter Key
链接到Next
按钮。
如何访问和覆盖 中的BACK
和NEXT
按钮FINISH
的属性QWizard
?