import sys
from inspect import getdoc
from PySide6.QtWidgets import (QApplication, 
    QWidget, QPushButton, QVBoxLayout, QHBoxLayout,
    QLabel)


class Window(QWidget):
    
    def __init__(self):

        super().__init__()
        
        layout = QVBoxLayout()
        self.setLayout(layout)
        self.conn_count = 0
        
        h_layout1 = QHBoxLayout()
        
        self.button = QPushButton('Click me!')
        self.label = QLabel('Connections? 0')
        
        h_layout1.addWidget(self.button)
        h_layout1.addWidget(self.label)
        
        layout.addLayout(h_layout1)
        
        h_layout2 = QHBoxLayout()
        
        connect_button = QPushButton('Connect')
        connect_button.clicked.connect(self.on_connect_clicked)
        
        disconnect_button = QPushButton('Disconnect')
        disconnect_button.clicked.connect(self.on_disconnect_clicked)
        
        h_layout2.addWidget(connect_button)
        h_layout2.addWidget(disconnect_button)
        
        layout.addLayout(h_layout2)
    
    def on_button_clicked(self):
        print('Button clicked')
        
    def on_connect_clicked(self):
        self.button.clicked.connect(self.on_button_clicked)
        self.conn_count += 1
        self.label.setText('Connections: ' + str(self.conn_count))
        print(self.conn_count)
        
    def on_disconnect_clicked(self):
        ok =  self.button.clicked.disconnect(self.on_button_clicked)
        if ok:
            self.conn_count -= 1
            self.label.setText('Connections: ' + str(self.conn_count))
        else:
            print('Failed to disconnect')
        print(self.conn_count)
   

if __name__ == '__main__':
    
    app = QApplication(sys.argv)

    main_window = Window()
    main_window.show()
    
    sys.exit(app.exec())
