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

C

cheirasacan

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 iput it out of the function. ¿ what am i doing wrong ? Thanks.
 
M

MRAB

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).
 
C

cheirasacan

El martes, 7 de mayo de 2013 12:53:25 UTC+2, MRAB escribió:
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 tofix 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.
 
M

MRAB

El martes, 7 de mayo de 2013 12:53:25 UTC+2, MRAB escribió:

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.
Presumably the details of the window are (or will be) hidden away in a
class, so you could make 'file' an attribute of an instance.

Also, please read this:

http://wiki.python.org/moin/GoogleGroupsPython

because gmail insists on adding extra linebreaks, which can be somewhat
annoying.
 
V

Vincent Vande Vyvre

Hi,

I've one machine with python3.2 (on Ubuntu 12.04), in the folder
/usr/lib/python3.2 it is a subfolder dist-packages, maybe created at the
install or created when I've installed the binding pyexiv2, I don't know.

Now, I've installed a new machine, again with Ubuntu 12.04 and therefore
python3.2.
This new install hasn't any *-packages subfolder in /usr/lib/python3.2

Today I install PyQt5, the make and make install are executed without
error but when I try:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PyQt5

I go to check in /usr/lib/python3.2 and I see the install of PyQt5 has
created a subfolder site-packages.
Is this naming dist-packages/site-packages critical for Python? (My
intuition is yes!)

So, I've tried with:and, also, created a file __init__.py in /site-packages but that's not
solved the problem.

Thanks for your advices.
 
C

cheirasacan

El martes, 7 de mayo de 2013 16:57:59 UTC+2, MRAB escribió:
Presumably the details of the window are (or will be) hidden away in a

class, so you could make 'file' an attribute of an instance.



Also, please read this:



http://wiki.python.org/moin/GoogleGroupsPython



because gmail insists on adding extra linebreaks, which can be somewhat

annoying.

The reply is very useful. I will keep learning.
Thanks for all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,520
Members
44,996
Latest member
rainocode

Latest Threads

Top