프로그래밍/PyQt5 GUI

15. QPushButton

디아블로 2021. 7. 23. 11:18

QPushButton 클래스의 자주 사용하는 메서드와 시그널을 아래 표에서 확인 바랍니다. (QPushButton 공식 문서) 참고

자주 사용하는 메서드

메서드 설명
setCheckable() True 설정 시, 누른 상태와 그렇지 않은 상태를 구분합니다.
toggle() 상태를 바꿉니다.
setIcon() 버튼의 아이콘을 설정합니다.
setEnabled() False 설정 시, 버튼을 사용할 수 없습니다.
isCheceked() 버튼의 선택 여부를 반환합니다.
setText() 버튼에 표시될 텍스트를 설정합니다.
text() 버튼에 표시된 텍스트를 반환합니다.

자주 사용하는 시그널

시그널 설명
clicked() 버튼을 클릭할 때 발생합니다.
pressed() 버튼이 눌렸을 때 발생합니다.
released() 버튼을 눌렀다 뗄 때 발생합니다.
toggled() 버튼의 상태가 바뀔 때 발생합니다.

15-1 예제: ex14

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        btn1 = QPushButton('&Button1', self)
        btn1.setCheckable(True)
        btn1.toggle()

        btn2 = QPushButton(self)
        btn2.setText('Button&2')

        btn3 = QPushButton('Button3', self)
        btn3.setEnabled(False)

        vbox = QVBoxLayout()
        vbox.addWidget(btn1)
        vbox.addWidget(btn2)
        vbox.addWidget(btn3)

        self.setLayout(vbox)
        self.setWindowTitle('QPushButton')
        self.setGeometry(300, 300, 300, 200)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

15-2 설명

btn1 = QPushButton('&Button1', self)
btn1.setCheckable(True)
btn1.toggle()
  • QPushButton의 첫 번째 파라미터는 버튼에 나타날 텍스트, 두 번째 파라미터는 버튼이 속할 위젯을 지정합니다.
  • 단축키(shortcut)를 지정하고 싶으면 해당 문자 앞에 ampersand('&')를 넣어주면 됩니다. 이 버튼의 단축키는 'Alt+B' 입니다.
  • setCheckable() 을 True로 설정해주면, 선택되거나 선택되지 않은 상태를 유지할 수 있게 됩니다.
  • toggle() 메서드를 호출하면 버튼의 상태가 바뀌게 됩니다. 
btn2 = QPushButton(self)
btn2.setText('Button&2')
  • setText() 메서드로 버튼에 표시될 텍스트를 지정합니다.
  • 이 버튼의 단축키는 'Alt+2'가 됩니다.
btn3 = QPushButton('Button3', self)
btn3.setEnabled(False)
  • setEnabled() 를 False로 설정하여 버튼을 비활성화 합니다.

15-3 결과