PyQt: "AttributeError: 'PlayerWindow' object has no attribute 'exec_'" when opening second window

PyQt: "AttributeError: 'PlayerWindow' object has no attribute 'exec_'" when opening second window

我正在使用 PyQt5 编写 GUI。在某些时候,我试图从主 window 打开辅助 windows。我的第二个 windows 是 class PlayerWindow.PlayerWindow 继承自 QWidget。打开 windows 的代码行是:

newWindow = PlayerWindow.PlayerWindow( self.playerUrl)
newWindow.show()
newWindow.exec_()

代码按照我希望的方式运行,但我收到错误消息:

AttributeError: 'PlayerWindow' object has no attribute 'exec_'

我怎样才能避免这种情况?

好的,我发现了我的错误。 如果 window 没有存储为主 window 实例的属性,则会被垃圾收集。所以改为:

self.newWindow = PlayerWindow.PlayerWindow( self.playerUrl)
self.newWindow.show()

exec 行导致禁止垃圾回收的异常。