PyQt5:对话框不显示任何窗口的背景颜色

问题描述 投票:0回答:0

我是 PyQt5 的初学者,我在学校的期末项目中使用它。我的项目是一个非常简单的客户端-服务器项目,具有两个客户端的简单游戏和多个窗口(菜单等)。我设计了每个独立的窗口并为其创建了一个类(使用 LoadUI),并使用 QStackedWidget 在窗口之间移动,所有这些都有效。

这就是我显示窗口的方式:

# CREATING GUI APP
app = QApplication(sys.argv)
# CREATING SCREENS
welcome_screen = WelcomeScreen()
login_screen = LoginScreen()
signup_screen = SignupScreen()
main_menu = None
edit_user_screen = None
topten_screen = None
friends_menu = None
play_menu = None
invitations_menu = None
# SETTING SHOWING WIDGET
widget = QStackedWidget()
widget.insertWidget(0, welcome_screen)
widget.insertWidget(1, login_screen)
widget.insertWidget(2, signup_screen)
widget.setFixedHeight(600)
widget.setFixedWidth(1000)
widget. Show()

try:
    app.exec_()
    logout()
except:
    print("end")

我的问题:经过长时间的游戏,当返回菜单和其他窗口时,窗口和按钮的背景颜色不显示,但标签颜色,例如,工作。它只是有时发生并且没有任何错误上升。 当我尝试创建窗口类的新实例并将它们添加到 QStackedWidget 时,背景颜色仍然不会显示,它还将标签的颜色更改为黑色,就像不使用 UI 文件一样,它分配默认颜色. 发生这种情况时,代码中的所有功能仍然有效,只是样式问题。 That's how the window that you return to after a game should look like:

That's how it looks if I only use .setCurrentIndex() to exit the game window:

That's how it looks if I create a new instance of this window's class and then use .setCurrentIndex() to display it

这是游戏窗口的部分代码:

class GameRoom(QDialog):
    def __init__(self, ID="", invitation=False, creator=True):
        super(GameRoom, self).__init__()
        loadUi("UIfiles\\gameroom.ui", self)
        # DEFINING BUTTONS
        self.exitbutton.clicked.connect(self.exit_room)

这是同一个类中exit_room和goback函数的相关部分:

def exit_room(self):
    print("enter exit_room")
    if self.over:  # when game is over
        self.goback()
        return

def goback(self):
    print("enter goback")
    widget.setCurrentIndex(7)

在这个类中处理游戏本身的函数很长所以我不能在这里包含它,但它不会改变其他窗口类的属性或 QStackedWindow 全局。

我尝试在退出游戏窗口之前将 QApplication.processEvents() 与 time.sleep(2) 一起使用,但它没有解决问题。 老实说,我不认为这是代码中的特定内容,因为它并非在所有情况下都会发生,而是因为游戏任务的长度或“繁重”。 我还是个初学者,我可能用错了。

python pyqt5 styles
© www.soinside.com 2019 - 2024. All rights reserved.