from qtpy.QtWidgets import QApplication, QPushButton app = QApplication([]) button = QPushButton('Click me') button.setCheckable(True) button.setChecked(True) button.show() app.exec_()
from qtpy.QtWidgets import QApplication, QPushButton, QHBoxLayout, QWidget app = QApplication([]) # create a horizontal layout layout = QHBoxLayout() # create 2 buttons button1 = QPushButton('Button 1') button1.setCheckable(True) button2 = QPushButton('Button 2') button2.setCheckable(True) # add buttons to layout layout.addWidget(button1) layout.addWidget(button2) # create container widget to attach layout to widget = QWidget() widget.setLayout(layout) widget.show() # function to handle button 1 toggling def button1_toggled(checked): button2.setChecked(checked) # connect button1 to signal and handling function button1.toggled.connect(button1_toggled) app.exec_()In this code, we create two QPushButton objects ('button1' and 'button2') and add them to a horizontal layout. The 'button1' is set to be checkable with setCheckable(), and a toggled signal is connected to a handling function 'button1_toggled' using toggled.connect(). When 'button1' is toggled, 'button2' is also toggled by setting its checked state with setChecked(). Python QtPy library provides a Python wrapper for the C++ Qt libraries which allows Python developers to easily create graphical user interfaces (GUIs) using PyQt or PySide.