프로그래밍/PyQt5 GUI
4. 창닫기
디아블로
2021. 7. 21. 17:06
4-1 예제: ex3
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton from PyQt5.QtCore import QCoreApplication class MyApp(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): btn = QPushButton('Quit', self) btn.setGeometry(50, 50, 100, 100) btn.resize(btn.sizeHint()) btn.clicked.connect(QCoreApplication.instance().quit) self.setWindowTitle('Quit Button') self.setGeometry(300, 300, 300, 200) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = MyApp() sys.exit(app.exec_()) |
4-2 설명
btn = QPushButton('Quit', self) |
- 프로그램을 종료할 버튼을 하나 만듭니다.
- btn 은 QPushButton 클래스의 인스턴스입니다.
- QPushButton 생성자의 첫번째 파라미터에는 버튼에 표시될 텍스트를 입력하고, 두 번째 파라미터에는 버튼이 위치할 부모 위젯을 입력합니다.
- QPushButton에 대한 자세한 설명은 QPushButton 문서를 참고하세요
btn.clicked.connect(QCoreApplication.instance().quit) |
- btn을 클릭하면 clicked 시그널이 생성됩니다.
- QCoreApplication.instance() 메서드는 현재 인스턴스 (MyApp)을 반환합니다.
- clicked 시그널은 QCoreApplication.instance().quit 슬롯에 연결됩니다.
4-3 결과