signals_and_slots_1c.py 729 B

12345678910111213141516171819202122232425262728293031323334
  1. import sys
  2. from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
  3. class MainWindow(QMainWindow):
  4. def __init__(self):
  5. super().__init__()
  6. self.button_is_checked = True
  7. self.setWindowTitle("My App")
  8. button = QPushButton("Press Me!")
  9. button.setCheckable(True)
  10. button.clicked.connect(self.the_button_was_toggled)
  11. button.setChecked(self.button_is_checked)
  12. # Set the central widget of the window
  13. self.setCentralWidget(button)
  14. def the_button_was_toggled(self, is_checked):
  15. self.button_is_checked = is_checked
  16. print(self.button_is_checked)
  17. app = QApplication(sys.argv)
  18. window = MainWindow()
  19. window.show()
  20. app.exec_()