signals_and_slots_3.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import sys
  2. from random import choice
  3. from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
  4. window_titles = [
  5. "My App",
  6. "My App",
  7. "Still My App",
  8. "Still My App",
  9. "What on earth",
  10. "What on earth",
  11. "This is surprising",
  12. "This is surprising",
  13. "Something went wrong"
  14. ]
  15. class MainWindow(QMainWindow):
  16. def __init__(self):
  17. super().__init__()
  18. self.n_times_clicked = 0
  19. self.setWindowTitle("My App")
  20. self.button = QPushButton("Press Me!")
  21. self.button.clicked.connect(self.the_button_was_clicked)
  22. self.windowTitleChanged.connect(self.the_window_tile_changed)
  23. # Set the central widget of the window
  24. self.setCentralWidget(self.button)
  25. def the_button_was_clicked(self):
  26. print("Clicked.")
  27. new_window_tile = choice(window_titles)
  28. print("Setting title: %s" % new_window_tile)
  29. self.setWindowTitle(new_window_tile)
  30. def the_window_tile_changed(self, window_tile):
  31. print("Window title changed: %s" % window_tile)
  32. if window_tile == "Something went wrong":
  33. self.button.setDisabled(True)
  34. app = QApplication(sys.argv)
  35. window = MainWindow()
  36. window.show()
  37. app.exec_()