[Python] How can I close and open a new window? [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I try to close and open a windows using PyQt5 and the following code:

    import PyQt5.QtWidgets as qtw
    from PyQt5 import uic
    import sys
    import os

    class Window2(qtw.QMainWindow):
    def __init__(self):
    super().__init__()
    uic.loadUi("window2.ui",self)

    class MainWindow(qtw.QMainWindow):
    def __init__(self):
    super().__init__()
    uic.loadUi("selectWindow.ui",self)
    self.path = os.path.abspath(os.path.dirname(sys.argv[0]))
    self.pbStart.clicked.connect(self.showData)

    def showData(self):
    print(self.leInput.text())
    self.w = Window2()
    self.w.show()
    self.hide()

    if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())


    The first windows appears, I can enter some value and press the button, but then I get the following error:

    $ python selectWindow.py
    3
    Traceback (most recent call last):
    File "D:\DEV\Fiverr2024\TRY\artfulrooms\selectWindow.py", line 20, in showData
    self.w = Window2()
    ^^^^^^^^^
    File "D:\DEV\Fiverr2024\TRY\artfulrooms\selectWindow.py", line 9, in __init__
    uic.loadUi("window2.ui",self)
    File "D:\DEV\.venv\pyqt\Lib\site-packages\PyQt5\uic\__init__.py", line 241, in loadUi
    return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "D:\DEV\.venv\pyqt\Lib\site-packages\PyQt5\uic\Loader\loader.py", line 66, in loadUi
    return self.parse(filename, resource_suffix)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "D:\DEV\.venv\pyqt\Lib\site-packages\PyQt5\uic\uiparser.py", line 1037, in parse
    actor(elem)
    File "D:\DEV\.venv\pyqt\Lib\site-packages\PyQt5\uic\uiparser.py", line 822, in createUserInterface
    self.toplevelWidget = self.createToplevelWidget(cname, wname)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "D:\DEV\.venv\pyqt\Lib\site-packages\PyQt5\uic\Loader\loader.py", line 57, in createToplevelWidget
    raise TypeError(
    TypeError: ('Wrong base class of toplevel widget', (<class '__main__.Window2'>, 'QDialog'))


    How can I close the actual windows and open the new window2?

    Continue reading...

Compartilhe esta Página