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.


Over 10,000 developers have bought Create GUI Applications with Python & Qt!

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount with the code [[ couponCode ]] — Enjoy!

For [[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount with the code [[ couponCode ]] — Enjoy!

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