I am developing a simple PyQt5 application and I am trying to open a new window from a parent window with the following function:
def park(self, N):
from time_dialog import T_MainWindow
ui = T_MainWindow(self, N)
ui.show()
And the class I am trying to access is:
class T_MainWindow(QtWidgets.QWidget):
def __init__(self, parent, N):
super().__init__()
self.PARENT = parent
self.N = N
self.setupUi()
Both these windows open up if run individually using:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = T_MainWindow("", "")
ui.show()
sys.exit(app.exec_())
When you open the T_MainWindow
in the second example, the ui
variable is global, so it does not get garbage-collected. But when you open it from the park
method, the ui
variable is local, so it will be garbage-collected when the method returns (and thus before the window is shown). To fix that, you can change the local variable to an attribute, so that a reference is kept for the window:
def park(self, N):
from time_dialog import T_MainWindow
self.t_window = T_MainWindow(self, N)
self.t_window.show()