[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Anyone tried Pygame + Tkinter?



Pete Shinners wrote:

> Ed Hunter wrote:
> 
>> My project uses Tkinter for historical reasons and until I create 
>> canvas with pygame, I can use from pygame only sound. So I setup sound 
>> and play() it, unfortunately, as soon as I return control to Tkinter's 
>> event loop, the sound is cancelled. Is there any way around it? Any 
>> suggestions?
> 
> 
> hmm, what platform is this? the SDL_mixer sound mixing is done in a 
> background thread (except os MacOS i suppose). so it shouldn't matter if 
> the code is in the tcl loop.


Linux Debian on x86.
pygame: 1.2-3
python: 2.0.1-1.1

python-tk: 1.5.2-18.4


(I do not have Tkinter for python 2.0, so I use the one for 1.5.2 with 
pygame. It complains about protocol version, but works)


> here's an idea. can you install some sort of 'idle' callback in tkinter 
> that just calls pygame.event.pump() ? or just put a callback on a timer 
> every second or so and have it call this function.


Unfortunately that requires a surface to be intialized. And since I use 
Tkinter for that ....

Anyway, it looks to me that the sound is cancelled, not only paused, 
because if I put sleep () in the idle callback it does not play anything 
neither - whereas when I put sleep() immediately after play(), it works

I have attached the example proggie - just click the window once it 
appears. You may need to fiddle with the paths in the beginning to get 
it run

Jarda Benkovsky
#!/usr/bin/env python2

import sys
import time

sys.path.append("/usr/lib/python1.5/lib-tk")
sys.path.append("/usr/lib/python1.5/lib-dynload")
file = "sounds/secosmic_lo.wav"

from Tkinter import *

import pygame.mixer, pygame.time
mixer = pygame.mixer
#time = pygame.time

class Pok:
    def __init__ (self, root):
        self.root = root
        
        mixer.init (11025)
        if not mixer.get_init ():
            raise SystemExit, 'Cannot Initialize Mixer'

        self.root.bind ('<1>', self.play)

    def play (self, ev):
        self.root.after_idle (self.idle)
        sound = mixer.Sound (file)

        channel = sound.play()
        #time.sleep (1)

    def idle (self):
        time.sleep (1)
        self.root.after_idle (self.idle)


root = Tk ()
obj = Pok (root)
root.mainloop ()