1. QPushButton

def __init__(self):
    QWidget.__init__(self, flags=Qt.Widget)

    self.btn_push = QPushButton()
    self.edt_number = QLineEdit()

    self.layout = QBoxLayout(QBoxLayout.TopToBottom, self)
    self.setLayout(self.layout)
    self.init_widget()

1) Signals

  • clicked(bool)
  • pressed
  • released
  • toggled(bool)
def init_widget(self):
   self.setWindowTitle("QPushButton Shortcut")
    self.click_cnt = 0
    self.edt_number.setText(str(self.click_cnt))
    self.layout.addWidget(self.edt_number)

    self.btn_push.setText("더하기 단축키(Alt + F7)")
    self.btn_push.pressed.connect(self.buttonClicked)
    self.btn_push.setShortcut("Alt+F7")
    self.layout.addWidget(self.btn_push)
        
def buttonClicked(self):
    self.click_cnt += 1
    self.edt_number.setText(str(self.click_cnt))

2) Shortcut 설정 (단축키)

    self.btn_push.setShortcut("Alt+F7")

3) Source

import sys
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QBoxLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt

__appname__ = "QPushButton Widget"

class QPushButtonWidgetForm(QWidget):
    def __init__(self):
        QWidget.__init__(self, flags=Qt.Widget)
        self.setWindowTitle(__appname__)
        
        self.btn_push = QPushButton()
        self.edt_number = QLineEdit()

        self.layout = QBoxLayout(QBoxLayout.TopToBottom, self)
        self.setLayout(self.layout)
        self.init_widget()

    def init_widget(self):
        self.setWindowTitle("QPushButton Shortcut")
        self.click_cnt = 0
        self.edt_number.setText(str(self.click_cnt))
        self.layout.addWidget(self.edt_number)

        self.btn_push.setText("더하기 단축키(Alt + F7)")
        self.btn_push.pressed.connect(self.buttonClicked)
        self.btn_push.setShortcut("Alt+F7")
        self.layout.addWidget(self.btn_push)

    def buttonClicked(self):
        self.click_cnt += 1
        self.edt_number.setText(str(self.click_cnt))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = QPushButtonWidgetForm()
    form.show()
    exit(app.exec_())

4) Result

 

2. QRadioButton

def __init__(self):
    QWidget.__init__(self, flags=Qt.Widget)
    self.setWindowTitle(__appname__)

    self.rb_1 = QRadioButton("Radio Button #1")
    self.rb_2 = QRadioButton("Radio Button #2")
    self.rb_3 = QRadioButton("Radio Button #3")
    self.rb_4 = QRadioButton("Radio Button #4")
    self.rb_5 = QRadioButton("Radio Button #5")

    self.layout = QBoxLayout(QBoxLayout.TopToBottom, self)
    self.layout.addWidget(self.rb_1)
    self.layout.addWidget(self.rb_2)
    self.layout.addWidget(self.rb_3)
    self.layout.addWidget(self.rb_4)
    self.layout.addWidget(self.rb_5)

    self.setLayout(self.layout)

1) Signals

  • clicked(bool)
  • pressed
  • released
  • toggled(bool)

2) Source

import sys
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QRadioButton
from PyQt5.QtWidgets import QBoxLayout
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt

__appname__ = "QRadioButton Widget"

class QRadioButtonWidgetForm(QWidget):
    def __init__(self):
        QWidget.__init__(self, flags=Qt.Widget)
        self.setWindowTitle(__appname__)

        self.rb_1 = QRadioButton("Radio Button #1")
        self.rb_2 = QRadioButton("Radio Button #2")
        self.rb_3 = QRadioButton("Radio Button #3")
        self.rb_4 = QRadioButton("Radio Button #4")
        self.rb_5 = QRadioButton("Radio Button #5")

        self.layout = QBoxLayout(QBoxLayout.TopToBottom, self)
        self.layout.addWidget(self.rb_1)
        self.layout.addWidget(self.rb_2)
        self.layout.addWidget(self.rb_3)
        self.layout.addWidget(self.rb_4)
        self.layout.addWidget(self.rb_5)

        self.setLayout(self.layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = QRadioButtonWidgetForm()
    form.show()
    exit(app.exec_())

3) Result

 

3. QCheckBox

def __init__(self):
    QWidget.__init__(self, flags=Qt.Widget)
    self.setWindowTitle(__appname__)

    self.cb_1 = QCheckBox("CheckBox #1")
    self.cb_2 = QCheckBox("CheckBox #2")
    self.cb_3 = QCheckBox("CheckBox #3")
    self.cb_4 = QCheckBox("CheckBox #4")
    self.cb_5 = QCheckBox("CheckBox #5")

    self.layout = QBoxLayout(QBoxLayout.TopToBottom, self)
    self.layout.addWidget(self.cb_1)
    self.layout.addWidget(self.cb_2)
    self.layout.addWidget(self.cb_3)
    self.layout.addWidget(self.cb_4)
    self.layout.addWidget(self.cb_5)

    self.setLayout(self.layout)

1) Signals

  • clicked(bool)
  • pressed
  • released
  • toggled(bool)
  • stateChanged(int)

2) Source

import sys
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QCheckBox
from PyQt5.QtWidgets import QBoxLayout
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt

__appname__ = "QCheckBox Widget"

class QCheckBoxWidgetForm(QWidget):
    def __init__(self):
        QWidget.__init__(self, flags=Qt.Widget)
        self.setWindowTitle(__appname__)

        self.cb_1 = QCheckBox("CheckBox #1")
        self.cb_2 = QCheckBox("CheckBox #2")
        self.cb_3 = QCheckBox("CheckBox #3")
        self.cb_4 = QCheckBox("CheckBox #4")
        self.cb_5 = QCheckBox("CheckBox #5")

        self.layout = QBoxLayout(QBoxLayout.TopToBottom, self)
        self.layout.addWidget(self.cb_1)
        self.layout.addWidget(self.cb_2)
        self.layout.addWidget(self.cb_3)
        self.layout.addWidget(self.cb_4)
        self.layout.addWidget(self.cb_5)

        self.setLayout(self.layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = QCheckBoxWidgetForm()
    form.show()
    exit(app.exec_())

3) Result

반응형

'Tech & Development > Programming Languages' 카테고리의 다른 글

[Python] 자료형 - 문자열 (String)  (0) 2021.05.18
[Python] 숫자형  (0) 2021.05.18
[ PyQt ] Signal & Slot  (0) 2021.01.30
Python 패키지 설치  (0) 2021.01.29
Markdown (마크다운) 문법 총정리  (4) 2021.01.29