windows_7.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import sys
  2. from random import randint
  3. from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget, QInputDialog, QLineEdit, \
  4. QLabel
  5. class AnotherWindow(QWidget):
  6. """
  7. This "window" is a Qwidget. If it has no parent, it
  8. will appear as a free-floating window.
  9. """
  10. def __init__(self):
  11. super().__init__()
  12. layout = QVBoxLayout()
  13. self.label = QLabel("Another Window %d" % randint(0, 100))
  14. layout.addWidget(self.label)
  15. self.setLayout(layout)
  16. class MainWindow(QMainWindow):
  17. def __init__(self):
  18. super().__init__()
  19. self.w = AnotherWindow()
  20. self.button = QPushButton("Press me fo Window!")
  21. self.button.clicked.connect(self.toggle_window)
  22. self.input = QLineEdit()
  23. self.input.textChanged.connect(self.w.label.setText)
  24. layout = QVBoxLayout()
  25. layout.addWidget(self.button)
  26. layout.addWidget(self.input)
  27. container = QWidget()
  28. container.setLayout(layout)
  29. self.setCentralWidget(container)
  30. def toggle_window(self, is_checked):
  31. if self.w.isVisible():
  32. self.w.hide()
  33. else:
  34. self.w.show()
  35. app = QApplication(sys.argv)
  36. window = MainWindow()
  37. window.show()
  38. app.exec_()