Skip to main content
 首页 » 编程设计

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

2024年10月24日37Terrylee

我是 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.