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(QDialog, self).__init__(*args, **kwargs)        
        self.setupUi(self)

class Window(QMainWindow, window_form):

    def __init__(self, *args, **kwargs):
        super(QMainWindow, self).__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.


The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

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.