473,218 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 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
sound.
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
no
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
Windows.
It does work on linux (fc5) but i don't know the name of any wav file
so
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
try:
import pygame.mixer as pgmixer
import pygame.time as pgtime
except ImportError:
print "pygame not found, using dummy audio"

gobject.threads_init()

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,
type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_NONE, flags=gtk.DIALOG_MODAL,
message_format=alertmsg)
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"
time.sleep(1)

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

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 ==
'win32'):
while audioalert():
self.cnt += 1
else:
while audiodummy():
self.cnt += 1

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

def audiodummy():
"""dummy audio playback"""
while t2.cnt >= 0 :
if t2.killaudio == 0:
print 'Beep!'
time.sleep(1)
else:
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\
"""
pgmixer.init(11025)
soundfile = os.path.join('.', 'tada.wav')
sound = pgmixer.Sound(soundfile)
channel = sound.play()
while channel.get_busy():
if t2.killaudio == 0:
pgtime.wait(1000)
else:
sound.stop()
pgmixer.quit()
return False
print '[DEBUG] loop number: ', t2.cnt
return True

t1 = ThreadOne()
t2 = ThreadTwo()

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

t1.start()
t2.start()

gtk.main()

print 't2.stop'
t2.stop()
print 't1.stop'
t1.stop()

##code end##

Thanks for your time.

Brendan Mchugh

Oct 25 '06 #1
0 1542

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vams | last post by:
Hi everyone, I got a little problem that I hope someone can help me out w/ or atleast point me in the right direction. Situation: - I have a python program that continuously runs and does some...
4
by: j_mckitrick | last post by:
Hi all. Here is a tiny container for one of each combo box, along with the glade file. Just 2 widgets, so hopefully not too large. How the heck do I get the selection from the ComboBox, as...
6
by: Mark Mitchell | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I have a program written in python that tells me it requires PyGTK, so I DLed, ./configure, make, make install and the process seems to go ok. Some...
0
by: Sebastien Aubry | last post by:
Hello, I am trying to embed a Tkinter widget (TkZinc, http://www.tkzinc.org) inside a PyGtk HMI. A guru gave me this advice: "I think you can do some magic using the XID of the Tkinter...
25
by: TPJ | last post by:
GUI's etc: PyGtk on Windows "(...) So if someone develops mainly for X and just wants to make sure that it is not impossible to run on Windows, you can use PyGTK. (...)", July 2nd, 1999 pyGTK...
6
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for...
1
by: krishnakant Mane | last post by:
hello, I will be writing some code in PyGTK to run on linux. but I want to know if there are any installers or distutils available for PyGTK on windows? I have heard that installing gimp or even...
2
by: Dmitry Teslenko | last post by:
Hello! I have simple chat application with pygtk UI. I want some event (for example update user list) to have place every n seconds. What's the best way to archive it? I tried threading.Timer but...
0
by: Michael Palmer | last post by:
On Sep 16, 12:30 pm, binaryjesus <coolman.gu...@gmail.comwrote: I haven't tried it myself, but I came across a blog post the other day that describes a way of building windows installers for...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.