why is there no reference between the app and the widget?

*爱你&永不变心* 提交于 2019-12-10 10:19:35

问题


This is a follow up to why will the application show after sys.exit command?

I'm following this tutorial:

http://zetcode.com/tutorials/pyqt4/firstprograms/

I modified the code slightly to test things

import sys
from PyQt4 import QtGui


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QWidget()
    w.resize(250,150)
    w.move(300,300)
    w.setWindowTitle("Title")
    w.show()
    sys.exit(app.exec_())

I'm confused as to why there isn't a reference of app and w i'd expect some kind of indication that w is a child or something of app.

I'm running it in Spyder and an IPython interpreter.


回答1:


For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. All windows are automatically managed by the Application.

You do have to initialize it and to get a hold of it to exec it though.

You must create the Application before any other GUI objects (because they are managed by it).

Detailed Description of the QApplication object



来源:https://stackoverflow.com/questions/15689313/why-is-there-no-reference-between-the-app-and-the-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!