Widget not able to create new widget ?
-
wrote on 23 Aug 2024, 07:03 last edited by
Hi guys,
I must be missing something, but what Im trying to do doesnt work.
See here.
import sys from PyQt5.QtCore import QSize from PyQt5.QtWidgets import QWidget, QMainWindow, QPushButton, QApplication class TestApplicationWindow(QWidget): def __init__(self, *args, **kwargs): QWidget.__init__(self, *args, **kwargs) self.resize(QSize(640, 480)) btn = QPushButton(self) btn.setText("Create window") btn.clicked.connect(self.create_window) def create_window(self): print("create_window") otherwindow = QWidget() otherwindow.resize(QSize(640, 480)) otherwindow.show() if __name__ == "__main__": print("Starting") app = QApplication(sys.argv) MainWindow = TestApplicationWindow() MainWindow.show() sys.exit(app.exec())
Can anyone explain me, why, when I click the button, the 2nd widget (otherwindow) isnt shown ?
-
wrote on 23 Aug 2024, 07:57 last edited by
Hmmz.... this is it.... https://www.pythonguis.com/tutorials/creating-multiple-windows/
Its created, but discarded.
Thanks all ! -
Hi guys,
I must be missing something, but what Im trying to do doesnt work.
See here.
import sys from PyQt5.QtCore import QSize from PyQt5.QtWidgets import QWidget, QMainWindow, QPushButton, QApplication class TestApplicationWindow(QWidget): def __init__(self, *args, **kwargs): QWidget.__init__(self, *args, **kwargs) self.resize(QSize(640, 480)) btn = QPushButton(self) btn.setText("Create window") btn.clicked.connect(self.create_window) def create_window(self): print("create_window") otherwindow = QWidget() otherwindow.resize(QSize(640, 480)) otherwindow.show() if __name__ == "__main__": print("Starting") app = QApplication(sys.argv) MainWindow = TestApplicationWindow() MainWindow.show() sys.exit(app.exec())
Can anyone explain me, why, when I click the button, the 2nd widget (otherwindow) isnt shown ?
wrote on 23 Aug 2024, 12:49 last edited by JonB@TheJester12121 said in Widget not able to create new widget ?:
otherwindow = QWidget()
This widget has no parent, it is not added to another widget directly or (better) on a layout belonging to parent widget. And no reference is kept to it in e.g.
self
. Hence Python will destroy it on exit fromcreate_window()
. Other languages (e.g. C++) would not. I don't know exactly where in https://www.pythonguis.com/tutorials/creating-multiple-windows/ you claim this precise code came from. -
1/3