Python
[pyQT5] pyQT Button. 버튼 클릭 이벤트 #pyQT Button
bryan.oh
2020. 8. 15. 14:09
반응형
pyQT Button Click
# 설치
2020/08/15 - [Python] - [pyQT5] pyQT install. pip 로 설치하기
# 소스
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# form
self.setWindowTitle('Hello Bryan')
self.center() # self.move(500,200) # 창 위치
self.resize(500, 200) # 창 크기
# 버튼 정의
btnRun = QPushButton("Run", self) # 버튼 텍스트
btnRun.move(20, 20) # 버튼 위치
btnRun.clicked.connect(self.btnRun_clicked) # 클릭 시 실행할 function
def btnRun_clicked(self):
QMessageBox.about(self, "message", "clicked")
'''
화면의 가운데로 띄우기
'''
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
app.exec_()
1. 버튼 생성 : btnRun = QPushButton("Run", self)
2. 위치 설정 : btnRun.move(20, 20)
3. 클릭 이벤트 설정 : btnRun.clicked.connect(self.btnRun_clicked)
# 참고
- 클릭 이벤트에서 버튼 객체 접근하기
def btnRun_clicked(self):
btn = self.sender()
- 버튼 텍스트 변경하기
def btnRun_clicked(self):
btn = self.sender()
btn.setText('runing')
- 버튼 disable 설정하기
def btnRun_clicked(self):
btn = self.sender()
btn.setText('runing')
btn.setDisabled(True) # 버튼 비활성화
728x90
반응형