Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>To take out the if condition:</strong></p> <p>If you don't want to check, then you should the initialize the variable since the very beginning. I don't know the class that you are using for <code>now_playing_object</code> but lets call it <code>PlayingObject</code>.</p> <p>Instead of:</p> <pre><code>now_playing_object = None </code></pre> <p>You should initialize it with :</p> <pre><code>#maybe needs parameters or simple use now_playing_object = ObjectProperty(None) now_playing_object = Sound() </code></pre> <p>And then you can take out the condition of my first answer (<code>cls.now_playing_object != None</code>) because the object is always initialize.</p> <p><strong>To take out the <code>@classmethod</code>:</strong></p> <p>I found interesting that you were using <code>@classmethod</code>. I have been couple of years in Python and honestly it was completely new for me. There are several answers depending what you are trying to do. I will assume that you are clear with the difference between <a href="http://en.wikipedia.org/wiki/Class_%28computer_programming%29" rel="nofollow">class</a> and <a href="http://en.wikipedia.org/wiki/Instance_%28computer_science%29" rel="nofollow">instance</a> in Oriented-Object Programming.</p> <p>You current code works if (1) you just have an instance of the <code>Gumb</code> class <strong>or</strong> (2) you have several instances of the <code>Gumb</code> class but all of them controls the same <code>soundfile</code>, <code>sound</code> and <code>now_playing_object</code>. </p> <p>Now, if you want your 15 <code>Gumb</code> buttons controlling different <code>soundfile</code>, <code>sound</code> and <code>now_playing_object</code>, then you shouldn't be using the @classmethod, neither the way you are declaring your attributes (because you are declaring them as class attributes and you need them to be part of the instance). The code will look more like this:</p> <pre><code>class Gumb(Button): # Constructor. def __init__(self, **kwargs): super(Gump, self).__init__(**kwargs) #This became attributes of the self.soundfile = StringProperty(None)\ self.sound = ObjectProperty(None) self.now_playing_object = ObjectProperty(None) def on_soundfile(self, instance, value): self.sound = SoundLoader.load(value) def on_press(self): if self.sound: self.play_sound() def play_sound(self): if self.now_playing_object.state != 'stop': self.now_playing_object.stop() # self.sound was your new_playing_object, since it is part of the instance # you don't need to send it as parameter self.now_playing_object = self.sound self.now_playing_object.play() </code></pre> <p>Is that more similar to what you want?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload