Python 如何运行使用Qt代码的.py文件?

Python 如何运行使用Qt代码的.py文件?,python,qt,user-interface,qt4,pyqt4,Python,Qt,User Interface,Qt4,Pyqt4,我不熟悉Python GUI编程和Qt。我已经了解到,如果我在Qt Designer中创建一个GUI应用程序(扩展名为.ui),我需要转换它。但是,如果我在记事本++中编写一个像下面这样的应用程序,那么如何运行该文件呢 import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI

我不熟悉Python GUI编程和Qt。我已经了解到,如果我在Qt Designer中创建一个GUI应用程序(扩展名为.ui),我需要转换它。但是,如果我在记事本++中编写一个像下面这样的应用程序,那么如何运行该文件呢

import sys
from PyQt4 import QtGui


class Example(QtGui.QWidget):

def __init__(self):
    super(Example, self).__init__()

    self.initUI()

def initUI(self):

    self.setGeometry(300, 300, 250, 150)
    self.setWindowTitle('Message box')
    self.show()

def closeEvent(self, event):

    reply = QtGui.QMessageBox.question(self, 'Message',
        "Are you sure to quit?", QtGui.QMessageBox.Yes |
        QtGui.QMessageBox.No, QtGui.QMessageBox.No)

    if reply == QtGui.QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()


def main():

app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())


if __name__ == '__main__':
main()
如何运行此脚本并获取GUI


谢谢

只需将其保存为“.py”文件,并以与运行所有普通python脚本相同的方式运行即可。仅仅因为它导入了qt库并不会改变这一点。

确保安装了pyqt4:
sudo apt get install python-qt4