[PyKDE] First Python QT app queries

Dave S eric3 at pusspaws.net
Wed Jul 19 19:04:23 BST 2006


> I guess you start it from a command line? Why not use eric's built in
> support for starting your script?

Eric cannot run more than one process at any one time. My script uses a daemon 
& a GUI front end so I have to execute it from the command line.

If you './dlg_livedata.py' you get a wierd 'X' cursor with dots - so not what 
I expected. If you 'python dlg_livedata.py' you get the expected GUI - I 
would love to know why this happens ?


>
> Anyway executing the script via ./<script> should work fine if the
> script is executable and the proper python is used. But the
> script you included below doesn't contain any shebang.
>
Full shebang supplied :) - to start ...

./test.py
Start a new shell ...
python dlg_livedata.py

And it works, sleeping, download, process data should alternately be 
highlighted.
(Please ignore my diagnostic prints - I am still learning)

The other bash errors I encounter have gone since a re-boot :)

> > (4) When I made my QlistBox I deleted all items. In qt designer preview
> > it looks perfect. When I write to the list box ie ...
> > 'win.listBox2.insertItem(mapdata)'
> > I get a thin line below the top line. How do I delete it ?
>
> I don't understand what your goal is? Do you want to replace an existing
> item with a new text? Then use changeText with a proper index. If you
> want to insert a new item at a specific position give an index number to
> the insertItem call. If you want something completely different, I think
> you should show us some code.

Code is below :) I want to be able to add lines of text - a scrolling log - 
adding to the bottom and the text scrolling up. 

I always end up with a thin blue box around the top line of text. How can I 
get rid of it ?

Any help greatly appreciated :)

Dave






*** dlg_livedata.py *********************************************************

#
#   Copyright (C) Dave Selby																	
#   jk at pusspaws.net																				
#																												
#   This program is free software; you can redistribute it and/or modify	
#   it under the terms of the GNU General Public License version 2		
#   as published by the Free Software Foundation (see COPYING)			
#                                                                         									
#   This program is distributed in the hope that it will be useful,       		
#   but WITHOUT ANY WARRANTY; without even the implied warranty of	
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	
#   GNU General Public License for more details.                          			
#

import sys, os, os.path, mmap
from qt import *
from frm_livedata import frm
from time import sleep

mmap_file = '/tmp/mmap_file'
mmap_size = 1000

class dlg_livedata(frm):
		
	def __init__(self,  *args):
		frm.__init__(self, *args)
		# Sets up QT  to call timerEvent() every 500ms
		self.timer = self.startTimer(250)
		# Setup the GUI display status flags
		self.daemon = self.sleeping = self.download = self.process = True
		
	def timerEvent(self, ev):
		"""
		Executes every 250ms and checks for 'live_datad' daemon. If present checks 
status of QT attribute
		and changes as necessary
		"""
		self.checkDaemon()
		self.checkMap()
		
		
	def checkDaemon(self):
		"""
		Checks for 'live_datad' daemon. If present checks status of QT attribute
		and changes as necessary
		"""
		# Check the output of 'ps ax' for 'live_datad'
		stdout = os.popen("ps ax","r")
		sys.stdout.flush()
		
		running = False		
		while True:
			line = stdout.readline()
			if not line: 
				break
			if line.find('test.py') != -1:
				running = True		
				
		# Flip the daemon QT attribute if necessary 
		if running and not self.daemon:
			self.daemon = True
			win.frame3.setPaletteBackgroundColor(QColor(239,49,28))
			win.textLabel3.setEnabled(1)
			
		elif not running and self.daemon:
			self.daemon = False
			win.frame3.setPaletteBackgroundColor(QColor(239,239,239))
			win.textLabel3.setEnabled(0)
			
	def checkMap(self):
		"""
		Checks for IPC from live_datad. If present checks status of QT attributes
		and changes as necessary
		"""
		
		# Read data from mmap
		f = os.open(mmap_file,  os.O_RDWR)
		map = mmap.mmap(f, mmap_size, mmap.MAP_SHARED, mmap.PROT_READ | 
mmap.PROT_WRITE)
		mapdata = map.readline()[:-1]
		print mapdata, len(mapdata)
		# Reset the memory pointer to the start
		map.seek(0)
		# Optional - blanks the map
		map.write('\n')
		map.seek(0)
		map.flush()
		
		map.close()
		os.close(f)

		if mapdata == '':
			print 'null string detected'
			
		elif mapdata == "sleep":	
			win.frame3_2.setPaletteBackgroundColor(QColor(194,239,135))
			win.textLabel3_2.setEnabled(1)
			win.frame3_2_2.setPaletteBackgroundColor(QColor(239,239,239))
			win.textLabel3_3.setEnabled(0)
			win.frame3_2_3.setPaletteBackgroundColor(QColor(239,239,239))
			win.textLabel3_4.setEnabled(0)		

		elif mapdata == "download":
			win.frame3_2.setPaletteBackgroundColor(QColor(239,239,239))
			win.textLabel3_2.setEnabled(0)
			win.frame3_2_2.setPaletteBackgroundColor(QColor(194,239,135))
			win.textLabel3_3.setEnabled(1)
			win.frame3_2_3.setPaletteBackgroundColor(QColor(239,239,239))
			win.textLabel3_4.setEnabled(0)	
			
		elif mapdata == "process":
			win.frame3_2.setPaletteBackgroundColor(QColor(239,239,239))
			win.textLabel3_2.setEnabled(0)
			win.frame3_2_2.setPaletteBackgroundColor(QColor(239,239,239))
			win.textLabel3_3.setEnabled(0)
			win.frame3_2_3.setPaletteBackgroundColor(QColor(194,239,135))
			win.textLabel3_4.setEnabled(1)					

		else:
			print 'mapdata = ',mapdata
			win.listBox2.insertItem(mapdata)
			
			
			
if __name__ == '__main__':

	# Make a dummy file for mmap module
	if not os.path.isfile(mmap_file):
		f=open(mmap_file, 'w')
		f.write(' ' * mmap_size)
		f.close()

	app = QApplication(sys.argv)
	win = frm()
	dlg=dlg_livedata()
	app.setMainWidget(win)
	win.show()
	QObject.connect(app, SIGNAL('lastWindowClosed()'),app, SLOT('quit()'))
	app.exec_loop()




*** frm_livedata.py *********************************************************




# -*- coding: utf-8 -*-

# Form implementation generated from reading ui 
file '/home/ubuntu/python_develop/unison/DDC/QT/frm_livedata.ui'
#
# Created: Tue Jul 18 20:13:04 2006
#      by: The PyQt User Interface Compiler (pyuic) 3.15.1
#
# WARNING! All changes made in this file will be lost!


import sys
from qt import *


class frm(QDialog):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        if not name:
            self.setName("DDC:Daemon_Monitor")

        self.setEnabled(1)
        self.setPaletteForegroundColor(QColor(0,0,0))


        self.frame3_2 = QFrame(self,"frame3_2")
        self.frame3_2.setGeometry(QRect(20,100,100,60))
        self.frame3_2.setFrameShape(QFrame.StyledPanel)
        self.frame3_2.setFrameShadow(QFrame.Raised)

        self.textLabel3_2 = QLabel(self.frame3_2,"textLabel3_2")
        self.textLabel3_2.setEnabled(0)
        self.textLabel3_2.setGeometry(QRect(10,10,80,37))

        self.frame3_2_2 = QFrame(self,"frame3_2_2")
        self.frame3_2_2.setGeometry(QRect(20,170,100,60))
        self.frame3_2_2.setFrameShape(QFrame.StyledPanel)
        self.frame3_2_2.setFrameShadow(QFrame.Raised)

        self.textLabel3_3 = QLabel(self.frame3_2_2,"textLabel3_3")
        self.textLabel3_3.setEnabled(0)
        self.textLabel3_3.setGeometry(QRect(10,10,80,37))

        self.frame3_2_3 = QFrame(self,"frame3_2_3")
        self.frame3_2_3.setGeometry(QRect(20,240,100,60))
        self.frame3_2_3.setFrameShape(QFrame.StyledPanel)
        self.frame3_2_3.setFrameShadow(QFrame.Raised)

        self.textLabel3_4 = QLabel(self.frame3_2_3,"textLabel3_4")
        self.textLabel3_4.setEnabled(0)
        self.textLabel3_4.setGeometry(QRect(10,10,80,37))

        self.frame3 = QFrame(self,"frame3")
        self.frame3.setGeometry(QRect(20,30,100,60))
        self.frame3.setFrameShape(QFrame.StyledPanel)
        self.frame3.setFrameShadow(QFrame.Raised)

        self.textLabel3 = QLabel(self.frame3,"textLabel3")
        self.textLabel3.setEnabled(0)
        self.textLabel3.setGeometry(QRect(10,10,80,37))

        self.listBox2 = QListBox(self,"listBox2")
        self.listBox2.setGeometry(QRect(130,30,660,271))
        self.listBox2.setPaletteBackgroundColor(QColor(232,255,255))
        self.listBox2.setVariableWidth(1)

        self.languageChange()

        self.resize(QSize(814,332).expandedTo(self.minimumSizeHint()))
        self.clearWState(Qt.WState_Polished)


    def languageChange(self):
        self.setCaption(self.__tr("DDC Monitor","A monitor"))
        self.textLabel3_2.setText(self.__tr("<p align=\"center\">Sleeping\n"
"</p>"))
        self.textLabel3_3.setText(self.__tr("<p align=\"center\">Download\n"
"</p>"))
        self.textLabel3_4.setText(self.__tr("<p align=\"center\">Process\n"
"Data</p>"))
        self.textLabel3.setText(self.__tr("<p align=\"center\">Daemon\n"
"Running</p>"))


    def __tr(self,s,c = None):
        return qApp.translate("frm",s,c)

if __name__ == "__main__":
    a = QApplication(sys.argv)
    QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
    w = frm()
    a.setMainWidget(w)
    w.show()
    a.exec_loop()


*** test.py *********************************************************



#!/usr/bin/python
import mmap, os, time, os.path


mmap_file = '/tmp/mmap_file'
mmap_size = 1000

# Make a dummy file for mmap module
if not os.path.isfile(mmap_file):
	f=open(mmap_file, 'w')
	f.write(' ' * 1000)
	f.close()

while True:

	for x in ('sleep\n', 'download\n', 'process\n', 'wow some 
test\n', 'hello\n'):
		# Write data to mmap
		f = os.open(mmap_file,  os.O_RDWR)
		map = mmap.mmap(f, 1000, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE)
		print x
		map.write(x)
		# Reset the memory pointer to the start
		map.seek(0)
		map.flush()
		map.close()
		
		os.close(f)
		time.sleep(1)
		









More information about the PyQt mailing list