Question

I am new to Python GUI programming and Qt. I have learnt that if I create a GUI app in Qt Designer (with extension .ui), I need to convert it. But What if I write an app like the following one in, suppose, Notepad++ , then how can I run the file?

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()

How can I run this script and get the GUI ?

Thanks

Was it helpful?

Solution

just save it as a ".py" file and run it the same way that you run all of your normal python scripts. Just because it imports the qt libraries does not change that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top