Hi there,

I'm just a hobbyist teaching myself python but I am making progress with the help of "Rapid GUI Programming with Python and QT". I have written what is probably a very poorly designed programme which does just about what I want but when it's closed the process continues to run.

The steps are - monitor com port for signal, if received display a window. When I close the window though, the process continues to run.

I have tried every combination of exit functions I can think of but they either crash python.exe when the window closes, stop the programme doing what I want, or have no effect.

Here is the code, marked up with various comments - any help would be much appreciated:

Rob

# -*- coding: utf-8 -*-
"""
Created on Wed Jan 09 20:31:24 2013

@author: rl4
"""

import sys
import time
import serial
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sys import exit
#from PyQt4 import QtCore, QtGui

app_1 = QApplication(sys.argv)
port = "COM9"
ser = serial.Serial(port, 2400, timeout=2)
data = ser.read(4)
while len(data) < 4:
    print '<4'

    time.sleep(2)
    data = ser.read(4)

#sys.exit(app.exec_()) #if you have this line you don't get past it

from PyQt4 import QtCore, QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(300, 200)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.textBrowser = QtGui.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(30, 20, 256, 192))
        self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "***Window Name***", None, QtGui.QApplication.UnicodeUTF8))
        self.textBrowser.setHtml(QtGui.QApplication.translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">***widget message***</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))

if __name__ == "__main__":
    import sys
    from sys import exit
    app_2 = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()#this is what makes the window appear
    print '1'
    app_1.exec_() #without this python crashes when the window is closed
    print '2' #this prints as soon as window is closed
    ser.close()
    print '3' #this prints as soon as window is closed
    #sys.exit(app2.exec_())
    print '4' #this doesn't print if line above is running
    #exit(0) #makes python crash
    sys.exit(app_1.exec_()) #python crashes without this line
    print '5'#this line never print

Hi there, After a night's sleep I answered my own question - the first app wasn't necessary, removed that reference and all OK.

Thanks anyway if you read this!

Rob

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.