말랑말랑제리스타일

How to change QLineEditText using QPushbutton in PyQt5 본문

프로그래밍/파이썬

How to change QLineEditText using QPushbutton in PyQt5

제리제리 2022. 3. 11. 10:37

This example is change QLineEditText using QPushbutton click event in PyQt5

How to create PyQt5 UI

반응형

You can create a function for create UI using this source

from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 100, 300, 100)
        self.setWindowTitle("Button test") 
        self.initform()
        
        def initform(self):

        self.le_test = QLineEdit() #Create LineEdit

        self.btn_test = QPushButton() #Create Button
        self.btn_test.setText('Submit')

        self.hlayout1 = QHBoxLayout(self)
        self.hlayout1.addWidget(self.btn_test)
        self.hlayout1.addWidget(self.le_test)
 
app = QApplication(sys.argv)
root = Window()
root.show()
 
sys.exit(app.exec_())

You can create UI without BoxLayout. But, When you use QHBoxLayout, the layout is more clear.

I'll explain simply about this code.

You can create QLineEdit use the code where I checked as "Create LineEdit".

And also create QPushButton in the code where I check as "Create Button". And set button text as "Submit".

After that create QHBoxLayout for using horizontal box layout.

And you can include QLineEdit and QPushButton in QHBoxLayout.

If you create UI using this code, you can watch the window like this picture.

반응형

PyQt5 Button Test Window
PyQt5 Button Test Window

Create Button Event

You can create button with this python code.

    def test_click(self):
        self.le_test.setText("clicked")

This source means change QLineEdit text when you start this function.

(I will include full python source code at last of this page.)

And also you have to update btn_test in initform function using this source.

self.btn_test.clicked.connect(self.test_click)

Full Python Code for this document

반응형

This is full python code for this document.

from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 100, 300, 100)
        self.setWindowTitle("Button test") 
        self.initform()

    def test_click(self):
        self.le_test.setText("clicked")
    
    def initform(self):

        self.le_test = QLineEdit()

        self.btn_test = QPushButton()
        self.btn_test.setText('Submit')
        self.btn_test.clicked.connect(self.test_click)

        self.hlayout1 = QHBoxLayout(self)
        self.hlayout1.addWidget(self.btn_test)
        self.hlayout1.addWidget(self.le_test)
 
app = QApplication(sys.argv)
root = Window()
root.show()
 
sys.exit(app.exec_())

You can test this program with Python and PyQt5.

반응형
Comments