Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQT: how to open new window

Tags:

python

pyqt

pyqt4

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.

like image 449
user3060854 Avatar asked Apr 21 '16 11:04

user3060854


1 Answers

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()
like image 143
salomonderossi Avatar answered Oct 18 '22 11:10

salomonderossi