PySide2 - Main Window 未显示并显示 "Window is not responding"

PySide2 - Main Window not showing and saying "Window is not responding"

我在使用 pyside2 时遇到了一些问题...之前我让它响应,但我现在不能..我不知道为什么

这是一些代码:

main.py

app = c.QApplication(c.sys.argv)
    splash_pix = c.QPixmap("animation.gif")
    splash = c.SplashScreen("animation.gif", c.Qt.WindowStaysOnTopHint)
    splash.show()
    app.processEvents()
    screen = app.primaryScreen()
    size = screen.size()
    main_window = c.Main(x=(size.width() // 2) - (500 // 2), y=(size.height() // 2) - (500 // 2))
    splash.finish(main_window)
    c.time.sleep(0.01)
    main_window.show()
    c.sys.exit(app.exec_())

我导入了包 “主要”是class我做的


class Main(QWidget):
    def __init__(self, x, y, width=500, height=500, parent=None):
        super().__init__()
        self.x, self.y, self.width, self.height = x, y, width, height
        self.setStyleSheet("background-color: rgba(255, 255, 255, 1);")
        self.setWindowTitle("Test")
        self.setGeometry(self.x, self.y, self.width, self.height)
        self.addWidgets()

    def addWidgets(self):
        self.welcome_label = QLabel("Test")
        self.welcome_label.setStyleSheet("QLabel {qproperty-alignment: AlignCenter; font-size: 30px; font-weight: 600;}")

        self.register_button = QPushButton("Register")
        self.register_button.setStyleSheet("QPushButton {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0.273, stop: 0 rgba(85,172,238,1), stop: 1 rgba(79, 165, 240, 1)); border-radius: 20px; color: white; font: bold 18px; min-width: 5em; min-height: 1.8em; padding: 1px;} QPushButton:hover {background-color: rgba(69, 131, 186, 1);}")
        self.register_button.clicked.connect(self.on_register)

        self.login_button = QPushButton("Login")
        self.login_button.setStyleSheet("QPushButton {background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0.273, stop:0 rgba(85,172,238,1), stop:1 rgba(79, 165, 240, 1)); border-radius: 20px; color: white; font: bold 18px; min-width: 5em; min-height: 1.8em; padding: 1px;} QPushButton:hover {background-color: rgba(69, 131, 186, 1);}")
        self.login_button.clicked.connect(self.on_login)


        self.layout = QVBoxLayout()
        self.layout.addWidget(self.welcome_label)
        self.layout.addWidget(self.register_button)
        self.layout.addWidget(self.login_button)
        self.setLayout(self.layout)

好吧,没关系,我找到了答案...Big Sur 显然搞砸了 PyQT。 修复这里:https://forums.macrumors.com/threads/pyqt5-and-big-sur.2260773/ 基本上 运行 export QT_MAC_WANTS_LAYER=1 在 运行 程序之前。 好奇怪的bug..

我在 MacOS 11.0.1 Big Sur 和 PySide2 5.15.1 和 5.15.2 上遇到了同样的问题(互联网上提到它已针对 PyQt 5.15.2 修复,但显然情况并非如此对于 PySide2)。我用 QT_MAC_WANTS_LAYER 确认解决方案,我是这样做的:

import os
os.environ['QT_MAC_WANTS_LAYER'] = '1'