'How to show an UI in a thread in Pyside6?

I wanted to show other ui in a thread, but failed.
The code is similar to the following:

import sys
from PySide6 import QtCore, QtWidgets, QtGui
from threading import Thread

class MyWidget2(QtWidgets.QWidget):
    def __init__(self):
        super(MyWidget2, self).__init__()
        self.text = QtWidgets.QLabel("Widget2", alignment=QtCore.Qt.AlignCenter)
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addWidget(self.text)

class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.button = QtWidgets.QPushButton("Click me!")
        self.text = QtWidgets.QLabel("Widget", alignment=QtCore.Qt.AlignCenter)
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addWidget(self.text)
        self.layout.addWidget(self.button)
        self.widget2=MyWidget2()
        self.button.layout()
        self.button.clicked.connect(self.showwidget2())

    def  showwidget2(self):
        def run():
            self.widget2.show()
            self.widget2.resize(800, 600)
        thread=Thread(target=run)
        thread.start()




if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()
    sys.exit(app.exec())

if I use a thread to show an UI, the program will get stuck, I want to know what I can do.
Thx.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source