473,581 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(threa ding.Thread):
"""Dialog thread"""
stopthread = threading.Event ()
def __init__(self):
super(ThreadOne , self).__init__( )
self.dialog = gtk.MessageDial og(parent=None,
type=gtk.MESSAG E_INFO,
buttons=gtk.BUT TONS_NONE, flags=gtk.DIALO G_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(threa ding.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.ha s_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\Medi a\
"""
pgmixer.init(11 025)
soundfile = os.path.join('. ', 'tada.wav')
sound = pgmixer.Sound(s oundfile)
channel = sound.play()
while channel.get_bus y():
if t2.killaudio == 0:
pgtime.wait(100 0)
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.conn ect("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 1577

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

Similar topics

2
2821
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 tasks automatically on a daily basis. - Now, I want to create a gtk frontend that displays its status and provides an exit button so I don't need...
4
2591
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 opposed to the ComboEntryBox? I'm pulling my hair out over this one! jonathon
6
2036
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 errors but the process finishes so I assumed they weren't fatal errors. So now I attempt to run my python program and I get; $ gccp.py...
0
1666
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 widget's window to produce a GdkWindow" I wrote this little program, which gets the XID of the Tkinter window and uses it to create a gtk.gdk.Window.
25
4265
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 on Windows "(...) > can i use pyGTK under > Windows???
6
2857
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 example) or is doing some long calculations.
1
1768
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 pygtk is next to impossible and is very tedious if at all one gets success. can any one share their experiences with installing and running pygtk...
2
2112
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 result is following: all events wait till exit of gtk main loop and only then they occur. Thanks in advance
0
1777
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 pyGTK applications at http://unpythonic.blogspot.com/2007/07/pygtk-py2exe-and-inno-setup-for-single.html
0
7876
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7804
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7910
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.