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:
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.
PyQt6 Crash Course — a new tutorial in your Inbox every day
Beginner-focused crash course explaining the basics with hands-on examples.