Creating new windows on Qtdesigner based .py files

Hello guys.

I want to open new window from button. But my design comes from Qtdesigner.
The question about QtDesigner based codes!!!. The main problem is all those tactics are not working on qtdesigner based .py files.

For examples:

I put one button at Qtdesigner. There is nothing more.

Just i wanted, when i push the button launch the same window.
when i translate to python code, it gives me a class and an if structure.

Here i explained with photos: open new window in pyqt5 | Qt Forum

Because of this reason i copied the class and change name
class Ui_MainWindow(object): >>>>>>>>>>>>>>>class Ui_MainWindow2(object):

Now i have two classes. In first class i will tried to call second class.

This the first untouched code:(so long :wink: )

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'x.ui'

# Created by: PyQt5 UI code generator 5.15.2

# WARNING: Any manual changes made to this file will be lost when pyuic5 is

# run again. Do not edit this file unless you know what you are doing.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(260, 226)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(90, 120, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 260, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pushButton.setText(_translate("MainWindow", "PushButton"))


if **name** == "**main**":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

I just copied the class. Now i have two. And i added a simple def for calling the other class.

It’s offer me build init

5c7ff758-d900-4c9d-b168-771fa5302c85-image.png

I accepted

Finally any solution didn’t help me. Especially designer based codes hasn’t solution.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(260, 226)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(90, 120, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 260, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pushButton.setText(_translate("MainWindow", "PushButton"))


class Ui_MainWindow(object):
def **init**(self):
self.nextWindow = Ui_MainWindow2()


def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(260, 226)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(90, 120, 75, 23))
    self.pushButton.setObjectName("pushButton")
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 260, 21))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pushButton.setText(_translate("MainWindow", "PushButton"))

    self.pushButton.clicked.connect(self.launcher)

def launcher(self):
    self.nextWindow.show()


if **name** == "**main**":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

First up, it’s not a good idea to edit the Python files generated from Qt Designer. They are automatically generated from the UI file: once you change them, you can no longer change your UI in Qt Designer, without re-applying all the changes you have made.

The best way to do this is to import the class from the generated py file into your own code. For example, say you have a file named main.py which imports your Ui_MainWindow2 like follows (you’ll need to change the from import to your own filename).

from mainwindow import Ui_MainWindow2

You can then create a window the same way as you have at the bottom of your file

ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

To add custom behaviour onto your window class you need to subclass it. For example

class MyMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)

window = MyMainWindow()
window.show()
sys.exit(app.exec_())

You can add any custom behaviour in this subclass definition. So for example, to add your clicked handler, you could do.

class MyMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        
        set.pushButton.clicked.connect(self.launcher)

   def launcher(self):
         self.nextWindow = QtWidgets.QMainWindow()
          
window = MyMainWindow()
window.show()
sys.exit(app.exec_())

If you want to create an identical second window (I wouldn’t recommend this for a real app, but…)

class MyMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        
        set.pushButton.clicked.connect(self.launcher)

   def launcher(self):
         self.nextWindow = MyMainWindow2()
         self.nextWinodw.show()

class MyMainWindow2(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
          
window = MyMainWindow()
window.show()
sys.exit(app.exec_())

I think this introduction to using Qt Designer UIs will be useful as it has a few other options for using your UI files.

1 Like

IT’S WORKING!!! Thank you too much.

from PyQt5 import QtWidgets
import sys

app = QtWidgets.QApplication(sys.argv)

from mainWindow import Ui_MainWindow

class MyMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
_____def init(self, *args, **kwargs):
__________super().init(*args, **kwargs)
__________self.nextWindow = MyMainWindow2() #PCYCHARM offered to sent _init
__________self.setupUi(self)

__________self.pushButton.clicked.connect(self.launcher)

_____def launcher(self):
__________ self.nextWindow.show()

class MyMainWindow2(QtWidgets.QMainWindow, Ui_MainWindow):
_____def init(self, *args, **kwargs):
__________super().init(*args, **kwargs)
__________self.setupUi(self)

window = MyMainWindow()
window.show()
sys.exit(app.exec_())