| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import sys
- from random import randint
- from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget, QInputDialog, QLineEdit, QLabel
- class AnotherWindow(QWidget):
- """
- This "window" is a Qwidget. If it has no parent, it
- will appear as a free-floating window.
- """
- def __init__(self):
- super().__init__()
- layout = QVBoxLayout()
- self.label = QLabel("Another Window %d" % randint(0, 100))
- layout.addWidget(self.label)
- self.setLayout(layout)
- class MainWindow(QMainWindow):
- def __init__(self):
- super().__init__()
- self.w = None # No external window yet
- self.setWindowTitle("My App")
- layout = QVBoxLayout()
- self.button = QPushButton("Press me for a dialog!")
- self.button.clicked.connect(self.show_new_window)
- layout.addWidget(self.button)
- container = QWidget()
- container.setLayout(layout)
- self.setCentralWidget(container)
- def show_new_window(self, is_checked):
- print("Button clicked!", is_checked)
- if self.w is None:
- self.w = AnotherWindow()
- self.w.show()
- else:
- self.w.close()
- self.w = None # Discard reference, close window.
- app = QApplication(sys.argv)
- window = MainWindow()
- window.show()
- app.exec_()
|