[PyQt] PyQt , trhead , asyncronous gui (newbie needs help)

Massimo Di Stefano massimodisasha at yahoo.it
Wed Sep 24 03:45:03 BST 2008


Hi,
i've a problem, i'm tring to add a "start/stop" action
on a simple gui, i can generalize the problem using these code :


import sys, time, threading, Queue

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *



class GuiPart(QtGui.QWidget):
	def __init__(self, queue, endcommand, *args):
		QtGui.QWidget.__init__(self, *args)
		self.queue = queue
		# We show the result of the thread in the gui, instead of the console
		self.time = QtGui.QLCDNumber(10)
		self.timelabel = QtGui.QLabel("Time")
		self.timelabel.setFont(QtGui.QFont("Times", 14, QtGui.QFont.Bold))
		self.timelabel.setBuddy(self.time)
		
		self.exit = QtGui.QPushButton("Exit", self)
		self.connect(self.exit, QtCore.SIGNAL("clicked()"),QtGui.qApp,  
QtCore.SLOT("quit()"))
		
		self.stop = QtGui.QCheckBox('stop', self)
		self.stop.setCheckable(True)
		self.connect(self.stop, QtCore.SIGNAL('clicked()'),  
self.processIncoming)
		
		self.endcommand = endcommand
		self.timer = QtCore.QBasicTimer()
		self.step = 0;
		controlsLayout = QtGui.QGridLayout()
		controlsLayout.addWidget(self.time,0,1);
		controlsLayout.addWidget(self.timelabel,0,0);
		#controlsLayout.addWidget(self.start,1,0);
		controlsLayout.addWidget(self.stop,1,1);
		controlsLayout.addWidget(self.exit,2,1);
		self.setLayout(controlsLayout);
		
		
	def closeEvent(self, ev):
		self.endcommand()
		
		
					
	def processIncoming(self):
		if self.stop.isChecked():
			pass
		else:
			while self.queue.qsize():
				try:
					msg = self.queue.get(0)
					self.time.display(float(msg))
					print 1
				except Queue.Empty:
					pass

class ThreadedClient:
	def __init__(self):
		# Create the queue
		self.queue = Queue.Queue()
		
		# Set up the GUI part
		self.gui=GuiPart(self.queue, self.endApplication)
		self.gui.show()
		
		# A timer to periodically call periodicCall :-)
		self.timer = QTimer()
		QObject.connect(self.timer,SIGNAL("timeout()"),self.periodicCall)
		
		# Start the timer -- this replaces the initial call to periodicCall
		self.timer.start(100)
		
		# Set up the thread to do asynchronous I/O
		# More can be made if necessary
		self.running = 2
		self.thread1 = threading.Thread(target=self.workerThread1)
		
		self.thread1.start()
		
	def periodicCall(self):
		self.gui.processIncoming()
		if not self.running:
			root.quit()
			
	def endApplication(self):
		self.running = 0
				
	def workerThread1(self):
		utctime = 1
		for i in range(1, 200):
			msg = float(i)
			self.queue.put(float(msg))
			time.sleep(1)
			
root = QApplication(sys.argv)
client = ThreadedClient()
sys.exit(root.exec_())


######################################

in these code the code associated to the tread,
start when i run it

(these in not what i need, what i'm looking for is to be able to start  
stop it)

i tried to stop it using the checkbutton but it is not a good idea :-(
it stop the visualization ... but unchecking it ive all the (not  
sended data) all in one

googling and tring to have help on irc
some people point me to variouse solution, like :

use the python module "select"
give a look at the Qtimer class (specially the single shoot )

but rading the docs i have difficoult to undstand its usage


apologize me, but i'm not a programmer
i'm tring to learn python and computer scienze
by myself .. so i've some problems to undstand basic concept :-/
my knowledge is based on ..  reading example and source
but unluky i can't figure out the .. tread and the asyncronous  
programming

thanks for any help!
regards

Massimo.
Chiacchiera con i tuoi amici in tempo reale! 
 http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com 



More information about the PyQt mailing list