Header Ads

Header ADS

QLineEdit

PyQt5 : QLineEdit .

QLineEdit object is the most commonly used input field. It provides a box in which one line of text can be entered.
Entry box is important part of any GUI like label and Buttons .

In this Blog we will create entry boxes using QLineEdit and also work with its some methods .



So lets Code :

# PyQt5 : Dynamic Coding
# QLineEdit examples ...
from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow,QLineEdit
from PyQt5.QtGui import *
import sys

class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setGeometry(200,300,300,200)
        self.setWindowTitle('Dynamic coding')

        self.entry = QLineEdit(self)
        self.entry.setToolTip('Enter your Name')
        self.entry.adjustSize()

        self.entry.returnPressed.connect(self.func)
        self.entry.move(110,50)

        self.button = QPushButton('Button 1',self)
        self.button.adjustSize()
        self.button.move(120,100)
        self.button.clicked.connect(self.func)

    def func(self):
        self.button.setText(self.entry.text())
        self.button.adjustSize()




if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

OUTPUT :

Lets understand the code :

first we have to import QLineEdit class from QtWidgets and in the coding section i create a QLineEdit of name entry .we also add a tooltip in it which shows a message when we hover mouse cursor to it .
after that we use a method adjustSize( ) which work same as resize or sizeHint  method and at last use a returnPressed method which call a function when we press enter on Entry box / QLineEdit .

In the function section we use a method text ()  which is used to fetch the data inserted into the QLineEdit and then used the fetched data as a new string on button.

QLineEdit for password  :

# PyQt5 : Dynamic Coding
# QLineEdit examples ...
from PyQt5.QtWidgets import QPushButton, QApplication, QMainWindow, QLineEdit
from PyQt5.QtGui import *
import sys


class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setGeometry(200, 300, 300, 200)
        self.setWindowTitle('Dynamic coding')

        self.entry = QLineEdit(self)
        self.entry.setToolTip('Enter your Name')
        self.entry.adjustSize()

        self.entry.returnPressed.connect(self.func)
        self.entry.move(110, 50)
        self.entry.setEchoMode(QLineEdit.Password)
        self.button = QPushButton('Button 1', self)
        self.button.adjustSize()
        self.button.move(120, 100)
        self.button.clicked.connect(self.func)

    def func(self):
        self.button.setText(self.entry.text())
        self.button.adjustSize()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

you can use setEchoMode() method for making QlineEdit as a Passwoed box just pass QLineEdit.Password as a paratmeter to it .
Their are various parameter that you can pass , 
you can see them at the end of blog .

OUTPUT :


Use stylesheet with QLineEdit( ) :

# PyQt5 : Dynamic Coding
# QLineEdit examples ...
from PyQt5.QtWidgets import QPushButton, QApplication, QMainWindow, QLineEdit
from PyQt5.QtGui import *
import sys


class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setGeometry(200, 300, 300, 200)
        self.setWindowTitle('Dynamic coding')

        self.entry = QLineEdit(self)
        self.entry.setToolTip('Enter your Name')
        self.entry.adjustSize()
        self.entry.move(110, 50)
        self.entry.setStyleSheet("background-color:cyan")

        self.button = QPushButton('Button 1', self)
        self.button.adjustSize()
        self.button.move(120, 100)
        self.button.clicked.connect(self.func)

    def func(self):
        self.button.setText(self.entry.text())
        self.button.adjustSize()
        self.entry.clear()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

OUTPUT :



Important methods of QLineEdit class ...

setAlignment()
Aligns the text as per alignment constants
  • Qt.AlignLeft
  • Qt.AlignRight
  • Qt.AlignCenter
  • Qt.AlignJustify
clear() 
Erases the contents

setEchoMode()
Controls the appearance of the text inside the box.
Echomode values are:
  • QLineEdit.Normal
  • QLineEdit.NoEcho
  • QLineEdit.Password
  • QLineEdit.PasswordEchoOnEdit
setMaxLength() 
Sets the maximum number of characters for input

setReadOnly()
 Makes the text box non-editable

setText() 
Programmatically sets the text

text() 
Retrives text in the field

setValidator()
Sets the validation rules. Available validators are
  • QIntValidator: Restricts input to integer
  • QDoubleValidator: Fraction part of number limited to specified decimals
  • QRegexpValidator: Checks input against a Regex expression

setInputMask() 
Applies mask of combination of characters for input

setFont() 
Displays the contents QFont object

cursorPositionChanged() 
Whenever the cursor moves

editingFinished() 
When you press ‘Enter’ or the field loses focus

returnPressed() 
When you press ‘Enter’

selectionChanged() 
Whenever the selected text changes

textChanged() 
As text in the box changes either by input or by programmatic means

textEdited() 
Whenever the text is edited




Our previous PyQt5 Blogs are :







This blog is contributed by Yogesh singh .
Please leave a feedback in comment section

 

No comments

Powered by Blogger.