将光标移动到第一个输入框pyqt5

时间:2021-06-01 11:53:38

标签: python pyqt5

我有一个使用不同数量的输入框的布局。

在返回按键事件时,我从所有输入框中获取值并重置这些值。 之后光标只放在最后一个输入框。如何将光标移动到第一个输入框。

这里使用了一些来自网络的其他示例。

from PyQt5.QtGui import QIntValidator,QDoubleValidator,QFont
from PyQt5.QtCore import Qt
import sys

class lineEditDemo(QWidget):
        def __init__(self,parent=None):
                super().__init__(parent)
                self.e1 = QLineEdit()
                self.e1.setValidator(QIntValidator())
                self.e1.setMaxLength(4)
                self.e1.setAlignment(Qt.AlignRight)
                self.e1.setFont(QFont("Arial",20))

                self.e2 = QLineEdit()
                self.e2.setValidator(QDoubleValidator(0.99,99.99,3))
                self.e3 = QLineEdit()
                self.e3.setInputMask("+99_9999_999999")

                self.e4 = QLineEdit()
                self.e4.textChanged.connect(self.textchanged)

                self.e5 = QLineEdit()
                self.e5.setEchoMode(QLineEdit.Password)

                self.e6 = QLineEdit("Hello PyQt5")
                self.e6.setReadOnly(True)
                self.e5.editingFinished.connect(self.enterPress)

                self.flo = QFormLayout()
                self.flo.addRow("integer validator",self.e1)
                self.flo.addRow("Double validator",self.e2)
                self.flo.addRow("Input Mask",self.e3)
                self.flo.addRow("Text changed",self.e4)
                self.flo.addRow("Password",self.e5)
                self.flo.addRow("Read Only",self.e6)

                self.setLayout(self.flo)
                self.setWindowTitle("QLineEdit Example")

        def textchanged(self,text):
                print("Changed: " + text)

        def enterPress(self):
                print("Enter pressed")
        def keyPressEvent(self, e):
            if e.key() in [Qt.Key_Return, Qt.Key_Enter]:
                self.e1.clear()
                self.e2.clear()
                self.e3.clear()
                self.e4.clear()
                self.e5.clear()




if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = lineEditDemo()
        win.show()
        sys.exit(app.exec_())```

When return key is pressed the cursor is at password feild. I want it to be at integer validator.

Image for clarification
[after entering the values][1]

After enter key pressed password is highlighted 
[after return key pressed][2]



  [1]: https://i.stack.imgur.com/bAvNn.png
  [2]: https://i.stack.imgur.com/GN9WX.png

1 个答案:

答案 0 :(得分:0)

e1.setFocus() 会将焦点放在 import matplotlib.pyplot as plt import numpy as np n_bins = 20 x = np.random.normal(0, 1, 1000) bin_edges = np.linspace(x.min(), x.max(), n_bins + 1) bin_values = np.histogram(x, bins=bin_edges)[0] cum_values = bin_values.cumsum() cum_values = cum_values / cum_values.max() fig, ax = plt.subplots(figsize=(10, 5)) plt.step(np.append(bin_edges, bin_edges[-1]), np.concatenate([[0], cum_values, [0]])) ax.set_prop_cycle(None) c = np.diff(cum_values) plt.step(np.append(bin_edges, bin_edges[-1]), np.concatenate([[0], c, [c[-1], 0]]), ls='--') plt.show() 上,这正是您要找的。

另一种方法是 self.focusNextChild(),如果当前光标在最后一个 LineEdit 中,它将把焦点放在你的第一个条目上(换句话说,只有当焦点已经在最后一个)。

请注意:如果按下回车键时当前光标不在最后一行编辑,则 e1 方法将聚焦下一行编辑而不是第一个。