Why sfml does not play the file inside a function in this python code?

cheirasacan at gmail.com cheirasacan at gmail.com
Tue May 7 09:56:28 EDT 2013


El martes, 7 de mayo de 2013 12:53:25 UTC+2, MRAB  escribió:
> On 07/05/2013 10:27, cheirasacan at gmail.com wrote:
> 
> > from tkinter import *
> 
> > import sfml
> 
> >
> 
> >
> 
> > window = Tk()
> 
> > window.minsize( 640, 480 )
> 
> >
> 
> >
> 
> > def sonido():
> 
> >      file = sfml.Music.from_file('poco.ogg')
> 
> >      file.play()
> 
> >
> 
> >
> 
> > test = Button ( window, text = 'Sound test', command=sonido )
> 
> > test.place ( x = 10, y = 60)
> 
> >
> 
> > window.mainloop()
> 
> >
> 
> >
> 
> >
> 
> >
> 
> > Using Windows 7, Python 3.3, sfml 1.3.0 library, the file it is played if i put it out of the function. � what am i doing wrong ? Thanks.
> 
> >
> 
> Perhaps what's happening is that sonido starts playing it and then
> 
> returns, meaning that there's no longer a reference to it ('file' is
> 
> local to the function), so it's collected by the garbage collector.
> 
> 
> 
> If that's the case, try keeping a reference to it, perhaps by making
> 
> 'file' global (in a simple program like this one, using global should
> 
> be OK).

Thanks. A global use of 'sonido' fix the problem. The garbage collector must be the point. But this code is part of a longer project. What can i do to fix it without the use of globals? I will use more functions like this, and i would like to keep learning python as well good programming methodology.
Thanks.



More information about the Python-list mailing list