0

I need open other window (configuration windows) in my app. Each window is a different class. I would like to know if I can group everything in the same class and open the window through a function.

from PyQt5 import QtWidgets, uic
import sys

class OpenMyApp(QtWidgets.QMainWindow):
    # My App
    pathUi = 'ui/interface.ui'
    pathStylesheet = 'ui/stylesheet.qss'

    def __init__(self):
        super(OpenMyApp, self).__init__()
        self.init_interface()

    def init_interface(self):
        self.ui = uic.loadUi(self.pathUi, self)
        style_app = self.pathStylesheet
        with open(style_app, "r") as fh:
            self.setStyleSheet(fh.read())
        self.ui.btn_openConfigApp.clicked.connect(self.open_config)

    def open_config(self):
       pass


class OpenMyConfigApp(QtWidgets.QMainWindow):
    # My Config App
    pathUi = 'ui/config-interface.ui'
    pathStylesheet = 'ui/stylesheet.qss'

    def __init__(self):
        super(OpenMyConfigApp, self).__init__()
        self.init_interface()

    def init_interface(self):
        self.ui = uic.loadUi(self.pathUi, self)
        style_app = self.pathStylesheet
        with open(style_app, "r") as fh:
            self.setStyleSheet(fh.read())


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = OpenMyApp()
    window.show()
    sys.exit(app.exec_())

enter image description here

Community
  • 1
  • 1
  • Just declare the Config class above your mainwindow class. In the constructor of your main window create an instance of the config class and store it in `self.config_widget`. Then you can simply call `self.config_widget.show()` in your open_config method. – Tim Woocker May 27 '19 at 14:17
  • 2
    Why would you want to have both widgets grouped into one class? You want 2 different windows, right? Each class represents 1 widget, so it's only logical to have 2 classes for them. – Tim Woocker May 27 '19 at 14:18
  • You should have only one `QMainWindow` in your app and it would be more logical if `OpenMyConfigApp` was a dialog box. After that, it's quite easy to open a dialog when you click on a button (with `QDialog::exec()`) – Dimitry Ernot May 27 '19 at 14:35

0 Answers0