Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

open new window in pyqt5



  • This post is deleted!

  • Moderators

    @Samuel-Bachorik
    i cant provide a python sample code.
    But basically a new window is shown for a widget without a parent or if it has a parent the widget's window flag property is set to Qt::Window.



  • @Samuel-Bachorik
    connect() the first window's button clicked signal to a slot which calls secondWindow.show(). This is one of the most basic things you can do in a Qt UI.

    firstWindow.button.clicked.connect(secondWindow.show)
    


  • This is how my first windows looks like is it possible to connect it by your way @JonB
    And my second window looks same just changed few things.

    class MyWindow(QMainWindow):
    counter = 0
    score = 0
    test = 0

    def __init__(self):
        self.question_prompts = [
    
        ]
    
        self.score = 0
        self.counter = 0
        self.test = 0
        super(MyWindow, self).__init__()
        self.setGeometry(200, 200, 500, 300)
        self.setWindowTitle("Samko")
    
        self.label = QtWidgets.QLabel(self)
        self.label.move(20, 30)
        self.label.setText("Login system")
        self.update()
    
        self.label1 = QtWidgets.QLabel(self)
        self.label1.move(151, 50)
        self.label1.setText("Do you have account\ny/n ?")
        self.update2()
    
        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("Enter")
        self.b1.move(149, 120)
        self.b1.resize(100, 32)
        self.b1.clicked.connect(self.clicked)
    
        self.textbox = QLineEdit(self)
        self.textbox.move(150, 90)
        self.textbox.resize(170, 28)
        self.textboxValue = self.textbox.text()
    
    
    
    
    def update(self):
        self.label.adjustSize()
    
    def update2(self):
        self.label1.adjustSize()
    

    def window():
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

    window()



  • @Samuel-Bachorik said in open new window in pyqt5:

    self.b1.clicked.connect(self.clicked)

    That will make clicking the button connect to whatever the MyWindow.clicked method does. Where is that in your code? The method needs to show secondWindow, or just make it self.b1.clicked.connect(secondWindow.show), as I said.


  • Banned

    @Samuel-Bachorik first are your two windows in separate files or are the in the same file?

    In either case can you post the code for both windows so that we can help with what the issue is

    Also do you want both windows open at the same time?



  • You can try subprocess to open your secondfile.py at the same time from the first window. :)


  • Banned

    Okay @Samuel-Bachorik while waiting for the answer to my previous question I have adjusted your code to be more Python-Qt-ish in that this version uses the dynamic layout system meant to be used with Python-Qt and I replaced your Text box with a set of Radio Buttons to make answering your question more straight forward. Look forward to hearing from you on the rest so that I can help you with doing that.

    #from PyQt5.QtCore    import *
    from PyQt5.QtGui     import *
    from PyQt5.QtWidgets import *
    
    # Do not use Global Variables unless absolutely necessary which 99.9% of the time
    # is not the case -- so the basic rule is never use them
    
    # You are not using the added functionality of a QMainWindow as such you ought not
    # use it as that is a lot of unnecessary overhead that you are including without
    # using and its best to just go with a simple QWidget
    class MainWindow(QWidget):
        def __init__(self):
          # Do not use super( ) in Python as it introduces 4 known issues that must be 
          # handled properly. Further there were still actual bugs within the usage of 
          # super( ) when used in Python. Yes while super( ) works fine within C++ (the
          # parent language to the python version) it does not work as seamlessly within 
          # Python due to the major differences between these 2 languages. Next the
          # reason it was created was to handle a rather rare issue and unless you are 
          # doing some complicated inheritance one will most likely never run into this 
          # extremely rare issue. However the 4 major issues that get included by using 
          # super( ) you are much more likely to occur than that rare issue its meant 
          # for to solve. Of course using the basic explicit method, as follows, does  
          # not cause these issues and is as just as simple as using `super( )` further 
          # you do not actually gain anything useful by using `super( )` in Python that 
          # could not be done in a much simpler and safer manner.
          # super(MyWindow, self).__init__()
            QWidget.__init__(self)
            self.setWindowTitle("Samko")
            Top=200; Left=200; Width=500; Hight=300
            self.setGeometry(Left, Top, Width, Hight)
    
          # These are not being used??
            self.score = 0
            self.counter = 0
            self.test = 0
            self.question_prompts = []
    
            HdrFont = QFont()
            HdrFont.setPointSize(16)
            HdrFont.setBold(True)
    
            self.lblHeadr = QLabel('Login System')
            self.lblHeadr.setFont(HdrFont)
    
            HBox1 = QHBoxLayout()
            HBox1.addWidget(self.lblHeadr)
            HBox1.addStretch(1)
    
            QstnFont = QFont()
            QstnFont.setPointSize(10)
            self.lblQustn = QLabel('Do you have an account?')
            self.lblQustn.setFont(QstnFont)
    
            HBox2 = QHBoxLayout()
            HBox2.addWidget(self.lblQustn)
            HBox2.addStretch(1)
    
            self.rdoYes = QRadioButton('Yes')
            self.rdoNo = QRadioButton('No')
    
            HBox3 = QHBoxLayout()
            HBox3.addWidget(self.rdoYes)
            HBox3.addWidget(QLabel('   '))
            HBox3.addWidget(self.rdoNo)
            HBox3.addStretch(1)
    
            self.btnSubmt = QPushButton(self)
            self.btnSubmt.setText("Submit")
            self.btnSubmt.move(149, 120)
            self.btnSubmt.resize(100, 32)
            self.btnSubmt.clicked.connect(self.Submit)
    
            HBox4 = QHBoxLayout()
            HBox4.addWidget(self.btnSubmt)
            HBox4.addStretch(1)
    
            self.lblMsg = QLabel()
    
            HBox5 = QHBoxLayout()
            HBox5.addWidget(self.lblMsg)
            HBox5.addStretch(1)
            
            VBox = QVBoxLayout()
            VBox.addLayout(HBox1)
            VBox.addWidget(QLabel(' '))
            VBox.addLayout(HBox2)
            VBox.addLayout(HBox3)
            VBox.addLayout(HBox4)
            VBox.addWidget(QLabel(' '))
            VBox.addLayout(HBox5)
            VBox.addStretch(1)
    
            self.setLayout(VBox)
    
        def Submit(self):
            if self.rdoYes.isChecked():
                self.lblMsg.setText('You Selected Yes')
            elif self.rdoNo.isChecked():
                self.lblMsg.setText('You Selected No')
            else:
                self.lblMsg.setText('Please Select Yes or No')
    
    if __name__ == "__main__":
      # To help with clarity I always call this the Main Event
      # Thread because that is what it is and this helps one
      # remember that when the go talking about the Main Event
      # Thread
        MainEventThread = QApplication([])
        MainEventThread.setStyle('Fusion')
    
        application = MainWindow()
        application.show()
        
      # This is the Qt5 way of doing this line if you are using
      # PySide2 you still have to use the old Qt4 style which is
      # sys.exit(MainEventHandler.exec_())
        MainEventThread.exec()
    


  • Wow thank you man ! For your help ! My program is working now !! Thank you a lot !



  • class MainWindow(QWidget):
    
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
    


  • Hello guys.

    I have another but so close question. The main problem is all this tactics is not working on qtdesigner based .py files.

    For examples:

    f3c9596c-f9d3-4d03-9d30-cd9ee264463d-image.png

    As you see there is just one buton. There is nothing more.

    Just i wanted, when i push the button launch the same things.

    This the first untouched code:

    -- 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
    7f8e0c3a-c900-4386-b617-6b5fe5432276-image.png

    It's offer me build init 5c7ff758-d900-4c9d-b168-771fa5302c85-image.png

    I aceepted
    889c01b3-661a-4de3-bd7d-86b445cad839-image.png

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


Log in to reply