Answer the question
In order to leave comments, you need to log in
How to create a new window in PyQt5?
Good afternoon.
There is a main window created in Designer with a button. All this is saved in the test.ui file.
Also, a new empty QWidget class window was created in the Designer and saved to the test2.ui file.
The new window file test2.py contains:
def new_form():
new_window = uic.loadUi("interface2.ui")
new_window.setWindowTitle("New form")
new_window.show()
import test2
main_window.pushButton.clicked.connect(test2.new_form)
Answer the question
In order to leave comments, you need to log in
reason: the new_window variable dies as soon as the new_form() function ends (why? for the glory of Satan, of course!)
Solutions:
1) create some kind of global variable. But global variables are bad form (don't know why):
new_window = None
def new_form():
global new_window
new_window = uic.loadUi("interface2.ui")
new_window.setWindowTitle("New form")
new_window.show()
мой_список_окон = []
def new_form():
global мой_список_окон
new_window = uic.loadUi("interface2.ui")
new_window.setWindowTitle("New form")
new_window.show()
мой_список_окон.append(new_window)
def new_form(parent):
new_window = uic.loadUi("interface2.ui")
new_window.setWindowTitle("New form")
new_window.show()
new_window.setParent(parent)
import test2
main_window.pushButton.clicked.connect(lambda: test2.new_form(main_window))
def new_form(parent):
parent.new_window = uic.loadUi("interface2.ui")
parent.new_window.setWindowTitle("New form")
parent.new_window.show()
import test2
main_window.pushButton.clicked.connect(lambda: test2.new_form(main_window))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question