Creating new windows on Qtdesigner based .py files

Heads up! You've already completed this tutorial.

yilmazbah10043 | 2021-02-22 14:23:48 UTC | #1

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: https://forum.qt.io/topic/113427/open-new-window-in-pyqt5/11

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: )

python
# -*- 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.

python
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_())

-------------------------

martin | 2021-03-01 07:02:26 UTC | #2

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

python

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_())

python

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_())

python

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)

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

def launcher(self): self.nextWindow = QtWidgets.QMainWindow()

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

python

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)

python
    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.


yilmazbah10043 | 2021-02-23 13:56:22 UTC | #3

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_())


Over 10,000 developers have bought Create GUI Applications with Python & Qt!

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount with the code [[ couponCode ]] — Enjoy!

For [[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount with the code [[ couponCode ]] — Enjoy!

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak