PyQT: how to open new window

First of all, similar questions have been answered before, yet I need some help with this one.

I have a window which contains one button (Class First) and I want on pressed, a second blank window to be appeared (Class Second).

I fiddled with the code copied from this question: PyQT on click open new window, and I wrote this code:

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

from PyQt4 import QtGui, QtCore
import sys
import design1, design2

class Second(QtGui.QMainWindow, design2.Ui_MainWindow):
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
        self.setupUi(self)

class First(QtGui.QMainWindow, design1.Ui_MainWindow):
    def __init__(self, parent=None):
        super(First, self).__init__(parent)
        self.setupUi(self)

        self.pushButton.clicked.connect(self.on_pushButton_clicked)
        self.dialog = Second(self)

    def on_pushButton_clicked(self):
        self.dialog.exec_()

def main():
    app = QtGui.QApplication(sys.argv)
    main = First()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

but on_pressed, this error message appears:

AttributeError: 'Second' object has no attribute 'exec_'

(design1 and design2 have been derived from the Qt designer.)

Any thought would be appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here I’m using the show method.

Here is a working example (derived from yours):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from PyQt4 import QtGui, QtCore
import sys
 
 
class Second(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
 
 
class First(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(First, self).__init__(parent)
        self.pushButton = QtGui.QPushButton("click me")
 
        self.setCentralWidget(self.pushButton)
 
        self.pushButton.clicked.connect(self.on_pushButton_clicked)
        self.dialog = Second(self)
 
    def on_pushButton_clicked(self):
        self.dialog.show()
 
 
def main():
    app = QtGui.QApplication(sys.argv)
    main = First()
    main.show()
    sys.exit(app.exec_())
 
if __name__ == '__main__':
    main()

If you need a new window every time you click the button, you can change the code that the dialog is created inside the on_pushButton_clicked method, like so:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from PyQt4 import QtGui, QtCore
import sys
 
 
class Second(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
 
 
class First(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(First, self).__init__(parent)
        self.pushButton = QtGui.QPushButton("click me")
 
        self.setCentralWidget(self.pushButton)
 
        self.pushButton.clicked.connect(self.on_pushButton_clicked)
        self.dialogs = list()
 
    def on_pushButton_clicked(self):
        dialog = Second(self)
        self.dialogs.append(dialog)
        dialog.show()
 
 
def main():
    app = QtGui.QApplication(sys.argv)
    main = First()
    main.show()
    sys.exit(app.exec_())
 
if __name__ == '__main__':
    main()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x