471,896 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,896 software developers and data experts.

pygtk and threading

Hello Everyone

I have been playing a little with pyGTK and threading to come up with
simple alert dialog which plays a sound in the background. The need for

threading came when in the first version i made, the gui would freeze
after clicking the close button until pygame finished playing the
In Windows it was acceptable because it could be ignored easily, but in

testing on linux (red hat 9) Gnome was throwing up a dialog for killing

hanging applications.

So now i have a threaded version that seems to work as it should
(except not
on rh9 beause gtk is to old, but that is getting upgraded to fc5/6 so
matter). So i was hoping to get some general comments about the code as
i am
not sure if i am doing the gtk or threading correctly (i hope the
pygame part
is simple enough that i have it right).
##code start##
#!/usr/bin/env python
Simple alert message dialog with sound.
Runs a dummy audio function if pygame is missing and not run on
It does work on linux (fc5) but i don't know the name of any wav file
i just copy the Windows tada.wav into same directory as script.

import sys
import threading
import time
import gobject
import gtk
import os.path
import pygame.mixer as pgmixer
import pygame.time as pgtime
except ImportError:
print "pygame not found, using dummy audio"


alertmsg = "Ding Dong"

class ThreadOne(threading.Thread):
"""Dialog thread"""
stopthread = threading.Event()
def __init__(self):
super(ThreadOne, self).__init__()
self.dialog = gtk.MessageDialog(parent=None,
buttons=gtk.BUTTONS_NONE, flags=gtk.DIALOG_MODAL,
self.quit = False

def run(self):
"""Run idle sleep loop while stopthread Event is not set"""
while not self.stopthread.isSet():
print self, " is running"

def stop(self):
"""Set stopthread Event"""

class ThreadTwo(threading.Thread):
"""Audio thread"""
stopthread = threading.Event()
def __init__(self):
super(ThreadTwo, self).__init__()
self.quit = False
self.cnt = 0
self.killaudio = 0

def run(self):
"""While audioalert returns True, increment counter"""
if (sys.modules.has_key('pygame')) and (sys.platform ==
while audioalert():
self.cnt += 1
while audiodummy():
self.cnt += 1

def stop(self):
"""Set killaudio flag to 1, set stopthread Event"""
self.killaudio = 1
print self, " stop was called"

def audiodummy():
"""dummy audio playback"""
while t2.cnt >= 0 :
if t2.killaudio == 0:
print 'Beep!'
print 'audiodummy done'
return False
return True

def audioalert():
Slightly expanded pygame/examples/sound.py
Playback sound and then test for audio channel usage
While channel is busy and while killaudio flag is set to 0
Check every second and return True at end of sound playback
When killaudio is set to 1 stop the playback and kill the mixer
Return False for ThreadTwo.run
NOTE: Copy tada.wav from X:\WINDOWS\Media\
soundfile = os.path.join('.', 'tada.wav')
sound = pgmixer.Sound(soundfile)
channel = sound.play()
while channel.get_busy():
if t2.killaudio == 0:
return False
print '[DEBUG] loop number: ', t2.cnt
return True

t1 = ThreadOne()
t2 = ThreadTwo()

mainwindow = t1.dialog
mainwindow.connect("destroy", lambda _: gtk.main_quit())



print 't2.stop'
print 't1.stop'

##code end##

Thanks for your time.

Brendan Mchugh

Oct 25 '06 #1
0 1481

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Vams | last post: by
4 posts views Thread by j_mckitrick | last post: by
6 posts views Thread by Mark Mitchell | last post: by
reply views Thread by Sebastien Aubry | last post: by
1 post views Thread by krishnakant Mane | last post: by
2 posts views Thread by Dmitry Teslenko | last post: by
reply views Thread by Michael Palmer | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.