PyQT: how to open a new window

First of all, such questions have already been answered, but I need help with this.

I have a window that contains one button (Class First), and I want to click, a second empty window (Class Second) will appear.

I used the code copied from this question: PyQT when I clicked on a new window that opens , 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, the following error message appears:

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

(design1 and design2 were obtained from Qt.)

Any thought would be appreciated.

+11
source share
2 answers

I think you should use 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 generated in the dialog inside the on_pushButton_clicked method

 #!/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() 
+17
source

In the above example, when starting the program, 18 MB of RAM is occupied. When you start the second screen, another 4 MB of RAM will be occupied. When we close the second page, the occupied memory will not be returned to Rome, and if the second page is opened a second time, then 4 MB of RAM will be taken up again. What is your solution for this problem?

0
source

Source: https://habr.com/ru/post/1247608/


All Articles