gpt4 book ai didi

python - Python (PyQT) 中的 GUI 窗口闪烁并立即关闭?

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:43 25 4
gpt4 key购买 nike

我是 PyQT 的新手,我刚刚开始通过这个视频了解它:https://www.youtube.com/watch?v=JBME1ZyHiP8

当我在我的 Ubuntu 14.04 上运行代码时

import sys
from PyQt4 import QtGui # Always have these two imports

app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(50,50,500,300)
window.setWindowTitle("PyQt start")

window.show()

装箱的窗口只是闪烁并关闭。我如何获得窗口留下来以便我可以与它互动? Youtube视频中的代码上面在 Windows 平台上进行了演示。我是否必须附加任何特定于我的代码的 Ubuntu?

最佳答案

您没有运行该应用程序,将此行添加到末尾:

sys.exit(app.exec_())

来自relevant documentation :

int QApplication.exec_ ()

Enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via quit()).

It is necessary to call this function to start event handling. The main event loop receives events from the window system and dispatches these to the application widgets.

Generally, no user interaction can take place before calling exec(). As a special case, modal widgets like QMessageBox can be used before calling exec(), because modal widgets call exec() to start a local event loop.

关于python - Python (PyQT) 中的 GUI 窗口闪烁并立即关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35051186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com