Python Forum

Full Version: problem with PyQt4
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
I have developed a python script which uses PyQt to draw an icon on the notification bar. When I run the script, it works fine, but when I try to run it as a windows service nothing appears on the notification bar. The service works but does not show the icon on the panel.

Is there any possibility to show this icon when it is run as a service?

this is the code

import win32service
import win32serviceutil
import win32event
import sys 
from PyQt4.QtGui import QApplication, QIcon, QMainWindow, QSystemTrayIcon

class PySvc(win32serviceutil.ServiceFramework):
    _svc_name_ = "BasculaSap"                         #nombre del servicio
    _svc_display_name_ = "BASCULA_SAP"      #nombre que se muestra en la lista de servicios de windows 	
    #Esta es la descripcion del servicio
     
    
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
    
    # el nucleo del servicio donde se ejecutan las ordenes    
    def SvcDoRun(self):
        import servicemanager
        import time
        import sys
        import serial
        import win32event
        import pymssql
        import math
        import sys 
        from PyQt4.QtGui import QApplication, QIcon, QMainWindow, QSystemTrayIcon
                  
        class MainWindow(QMainWindow):
            def __init__(self):
                QMainWindow.__init__(self)

        def chk_peticion_BBDD():
          
            try:
                conn=pymssql.connect(host=host_,user=user_,password=password_,database='sgci')
                cursor_= conn.cursor()
                cursor_.execute("select * from BASCULA_SGCI where bitVacio=0")
                nrow=0
                for row in cursor_:
                    nrow=nrow+1
                cursor_.close()		
                return nrow	
            except:
                return 0
        
        def upd_BBDD_Peso(peso):
            try:
                conn_w=pymssql.connect(host=host_,user=user_,password=password_,database='sgci')
                cursor_w= conn_w.cursor()
                pesoint = float(peso)
                print pesoint
                cursor_w.execute("update BASCULA_SGCI set peso=%f , bitVacio=1 where bitVacio = 0 ",(pesoint))
                conn_w.commit()
                cursor_w.close()
            except:
                return
	
        def isNumber(x):
            try:
                n=int(x)
                return 0 == n*0
            except:
                return False

        def	getWeight():
            try:
                ser = serial.Serial('COM1',9600,timeout=1,bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE)
                ser.open() 
                ser.flushInput()
                ser.flushOutput()
                ser.write(chr(22))
                time.sleep(2)
                peso=ser.readline()
                ser.write(chr(22))
                time.sleep(2)
                peso=ser.readline()
                #print "peso funcion " + peso
                #ser.close()
                return peso
            except:
                ser.close()
                return -1
        

        app = QApplication(sys.argv)
        window = MainWindow()
        window.systray = QSystemTrayIcon(QIcon("icon1.ico"),window)
       
        rc=None	
        i=0	
        while rc != win32event.WAIT_OBJECT_0:
            nrows_peticiones = chk_peticion_BBDD()
            if nrows_peticiones > 0:
                peso=getWeight() #ser.readline()
                if isNumber(peso):
                    print "peso ->" + peso.strip() + " Kg"
                    window.show()
                    upd_BBDD_Peso(peso)
            time.sleep(2)  
            if i>=1: 
	            window.systray.setIcon(QIcon("icon2.ico"))
            if i>=2:
                window.systray.setIcon(QIcon("icon1.ico"))
                i=0
            i=i+1
            rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)           
 
    def SvcStop(self):
        # tell the SCM we're shutting down
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)