# cPy qt_intro : button
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-01-25  initial release

from    PySide6.QtWidgets   import  QApplication, QWidget, QLabel, QPushButton

# ========================== demo 1  ==============================

# most simple usage of QPushButton
class Gui1(QWidget):

    def __init__(self):
        app = QApplication()            # Qt framework
        super().__init__()              # instantiate QWidget = GUI
        self.resize(500, 300)
        self.n   = 0                    # number of times button pressed
        self.btn = QPushButton('Press Me', self)
        self.btn.setGeometry(50, 50, 150, 25)
        self.btn.clicked.connect(self.onBtnClick)
        self.show()
        app.exec()


    def onBtnClick(self, sts):
        self.n += 1
        self.btn.setText(f'I am pressed {self.n} times')

Gui1()

# ========================== demo 2  ==============================

# two coloured buttons
class Gui2(QWidget):

    def __init__(self):
        app = QApplication()            # Qt framework
        super().__init__()              # instantiate QWidget = GUI
        self.resize(500, 300)
        btn = QPushButton('Red', self)
        btn.setGeometry(50, 50, 150, 35)
        btn.setStyleSheet('font-size: 20px; font-weight: bold; background-color: Blue')
        btn.clicked.connect(self.onBtnRedClick)
        btn = QPushButton('Blue', self)
        btn.setGeometry(300, 50, 150, 35)
        btn.setStyleSheet('font-size: 20px; font-weight: bold; background-color: Red')
        btn.clicked.connect(self.onBtnBlueClick)
        self.show()
        app.exec()


    def onBtnRedClick(self, sts):
        print('btn Red')


    def onBtnBlueClick(self, sts):
        print('btn Blue')


#Gui2()

# ========================== demo 3  ==============================

# 12 buttons in calculator style
class Gui3(QWidget):

    def __init__(self):
        app = QApplication()            			# Qt framework
        super().__init__()              			# instantiate QWidget = GUI
        self.resize(270, 340)
        self.lbl = QLabel('status', self)
        self.lbl.setGeometry(110, 20, 100, 20)		# button size
        for i, capt in enumerate('123456789-0#'):	# button caption
            x = 40 + (i %  3) * 70					# button position
            y = 50 + (i // 3) * 70
            btn = QPushButton(capt, self)
            btn.setGeometry(x, y, 50, 50)
            btn.setStyleSheet('font-size: 20px; font-weight: bold')
            btn.clicked.connect(self.onBtnClick)
        self.show()
        app.exec()


    # one handler for all buttons. Get button caption from self.sender()
    def onBtnClick(self, sts):
        txt = self.sender().text()
        self.lbl.setText(f'button: {txt}')


#Gui3()

# ============================== end =============================
