Opening a subwindow from a button event

Heads up! You've already completed this tutorial.

Cassio_Lemos | 2020-05-11 08:45:06 UTC | #1

Hello. I would like to ask how do i can open a subwindow from a button clicked event.

I learned from this tutorial that we can call our .ui files made on QtDesigner on a .py file and write only the events that we want to occour. However, i want to write a event from a clicked button that will call a second window, and would be great if this new subwindow would come from a different .ui file.

How do I do that?


Luca | 2020-05-20 18:51:02 UTC | #2

I wrote an example for you:

python
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

WINDOW_UI_FILE = "/home/user/window.ui"
DIALOG_UI_FILE = "/home/user/dialog.ui"

class Dialog(QDialog, dialog_form):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)

class Window(QMainWindow, window_form):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        self.pushButton_moveVariable.clicked.connect(self.show_dialog)

    def show_dialog(self):
        dialog = Dialog()
        dialog.exec_()

def main():

    app = QApplication(sys.argv)

    window_form, _ = uic.loadUiType(WINDOW_UI_FILE)
    dialog_form, _ = uic.loadUiType(DIALOG_UI_FILE)

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

If necessary, instead of a QDialog, it could be a QMainWindow.

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book


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

Opening a subwindow from a button event was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.