unicorn_slayer
unicorn_slayer

Reputation: 21

How do I automatically change focus between all the line edits in PyQT?

I have a simple encryption code generator in a program. When the user enters the first number, I'd like the focus to automatically change to the next line editor (see attached screenshot). I can't find any obvious method which can do this. I'm new to PyQT so I was wondering if anyone knows how this might be possible?

Thanks

Screenshot of my dialogue box

Upvotes: 1

Views: 1011

Answers (1)

Nicholas TJ
Nicholas TJ

Reputation: 1659

Here you go:

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize    


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(320, 240))    
        self.setWindowTitle("Keyword") 
        self.anchor = []

        # Line Edit 1
        self.nameLabel = QLabel(self)
        self.nameLabel.setText('#1')
        self.line1 = QLineEdit(self)
        self.line1.move(80, 20)
        self.line1.resize(200, 32)
        self.nameLabel.move(20, 20)
        self.anchor.append(self.line1)

        # Line Edit 2
        self.nameLabel2 = QLabel(self)
        self.nameLabel2.setText('#2')
        self.line2 = QLineEdit(self)
        self.line2.move(80, 60)
        self.line2.resize(200, 32)
        self.nameLabel2.move(20, 60)
        self.anchor.append(self.line2)

        # Line Edit 3
        self.nameLabel3 = QLabel(self)
        self.nameLabel3.setText('#3')
        self.line3 = QLineEdit(self)
        self.line3.move(80, 100)
        self.line3.resize(200, 32)
        self.nameLabel3.move(20, 100)
        self.anchor.append(self.line3)

        # Line Edit 4
        self.nameLabel4 = QLabel(self)
        self.nameLabel4.setText('#4')
        self.line4 = QLineEdit(self)
        self.line4.move(80, 140)
        self.line4.resize(200, 32)
        self.nameLabel4.move(20, 140)
        self.anchor.append(self.line4)

        # Button
        pybutton = QPushButton('OK', self)
        pybutton.clicked.connect(self.clickMethod)
        pybutton.resize(200,32)
        pybutton.move(80, 200)        

        # Connect
        self.line1.textEdited.connect(lambda state, x=1: self.checkDigit(x))
        self.line2.textEdited.connect(lambda state, x=2: self.checkDigit(x))
        self.line3.textEdited.connect(lambda state, x=3: self.checkDigit(x))
        self.line4.textEdited.connect(lambda state, x=4: self.checkDigit(x))

    def checkDigit(self, data):
        try:
            if (int(self.anchor[data-1].text()) < 10):
                self.anchor[data].setFocus()
        except:
            pass

    def clickMethod(self):
        print("Your four digits are:" + 
              " " + self.line1.text() + 
              " " + self.line2.text() +
              " " + self.line3.text() +
              " " + self.line4.text())

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit( app.exec_() )

Upvotes: 1

Related Questions