signals_and_slots_3.py 1.2 KB

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