Sunday, April 3, 2022

Textbox(QLineEdit) with PyQt6

 This demo program shows how to use Textbox(QlineEdit) and place it anywhere on a window detect when the "enter" key was pressed.


Output:



The code:


 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sys
import os
from PyQt6.QtWidgets import QApplication,  QWidget, QLineEdit, QLabel
from PyQt6 import QtCore, QtGui

class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()
        
 
        
        self.initUI()

    def initUI(self):

        self.line_edit = QLineEdit('Enter Text Here', self)
        self.line_edit.setGeometry(25, 25, 150, 40)

        self.linelabel = QLabel('Entered Text Here', self)
        self.linelabel.setGeometry(25, 35, 180, 120)
        self.linelabel.setWordWrap(True)
        
        self.line_edit.returnPressed.connect(lambda: self.do_action())

        self.setGeometry(25, 45, 350, 150)
        self.setWindowTitle('Post 9')
        self.show()
        
 
    def do_action(self):

        value = self.line_edit.text()
        self.linelabel.setText(value)
       
 
        


def main():

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

No comments:

Post a Comment