말랑말랑제리스타일
How to change QLineEditText using QPushbutton in PyQt5 본문
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.
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.
'프로그래밍 > 파이썬' 카테고리의 다른 글
구름 IDE에서 파이썬 PIL 이미지 미리보기 (0) | 2023.06.08 |
---|---|
파이썬으로 테서렉트 OCR로 웹 이미지에서 글자 추출하기 (0) | 2023.06.08 |
파이썬 PyQt5 로 윈폼 GUI 만들기 (0) | 2022.03.04 |
파이썬 lambda 함수의 뜻과 사용방법 (0) | 2022.02.16 |
파이썬 랜덤 숫자 뽑기로 중복 없이 숫자 뽑기 (0) | 2022.02.15 |