In this article we want to learn How to Create LineEdit in Python and PySide6, when we want to build graphical user interface (GUI) in Python, one of the most common widgets that you might want to include is LineEdit. LineEdit is a widget that allows the user to input and edit text in single line. In this article we are going to learn how to create LineEdit widget in Python using PySide6 library.
Setting up the environment
Before starting our coding let’s make sure that we have everything we need to create our LineEdit widget. first of all we need to install PySide6. we can do this by running following command in our terminal:
1 |
pip install PySide6 |
What is a LineEdit?
LineEdit is widget that allows the user to input text. it is commonly used for tasks such as entering usernames and passwords, searching for items in list or inputting data into database. the LineEdit widget is fundamental part of any GUI, and it is essential to know how to create one.
Creating a LineEdit in PySide6
for creating LineEdit widget in PySide6 we need to import QtWidgets module from PySide6 library. after that we have created new class that extends from QWidget class which is the base class for all GUI elements in PySide6.
1 2 3 4 5 6 7 8 9 10 11 |
from PySide6.QtWidgets import QWidget, QLineEdit class LineEditWidget(QWidget): def __init__(self): super().__init__() # create an instance of QLineEdit widget self.lineedit = QLineEdit(self) # set position of LinEdit widget self.lineedit.move(50, 50) # Set siz of the LineEdit widget self.lineedit.resize(200, 30) |
In the above code we have defined new class called LineEditWidget which extends from QWidget class. we have also created an instance of the QLineEdit widget and added to LineEditWidget using self.lineedit variable. and move() method sets position of the LineEdit widget and resize() method sets its size.
This is the complete code this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
from PySide6.QtWidgets import QWidget, QLineEdit from PySide6.QtWidgets import QApplication class LineEditWidget(QWidget): def __init__(self): super().__init__() # create an instance of QLineEdit widget self.lineedit = QLineEdit(self) # set the position of LineEdit widget self.lineedit.move(50, 50) # set size of LineEdit widget self.lineedit.resize(200, 30) #set default value for LineEdit widget self.lineedit.setText("Enter text here...") #connect a function to the textChanged signal self.lineedit.textChanged.connect(self.on_text_changed) def on_text_changed(self, text): """ slot function that is called when the text in the LineEdit widget changes. """ print(f"Text changed to: {text}") app = QApplication([]) widget = LineEditWidget() widget.show() app.exec() |
In the above code we have also included optional steps to set a default value for the LineEdit widget and connect function to textChanged signal which is emitted whenever text in the widget changes. on_text_changed function is slot function that is called whenever this signal is emitted and it simply prints out the new text.
also we have created QApplication object and used it to show the LineEditWidget. and app.exec() call starts the event loop, which is necessary for the GUI to function properly.
Run the complete code and this will be the result
Learn More on PySide6
- How to Create Label in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
In result we can say that Creating a LineEdit widget in PySide6 is simple process that can be done using few lines of code.