0
def OnClick1(self,event):
    song='maid.mp3'
    aplayer(song, play1 , 1, 0)

play1 is a global variable that is modified in another module. But the value of play1 is still the initial one which is 0. How to format the variable play1?

1
  • def SetVal(self, event): global t0, t4 state1 = str(self.rb1.GetValue()) print state1 state2 = str(self.rb2.GetValue()) print state2 state3 = str(self.rb3.GetValue()) print state3 state4 = str(self.rb4.GetValue()) print state4 if state1=="True": t0="audio2.mp3" if state2=="True": t0="audio1.mp3" if state3=="True": t4="audio2.mp3" if state4=="True": t4="audio1.mp3" Again the problem with the global variable. It says t0 and t4 are not defined. Please help Commented Jan 16, 2014 at 11:22

1 Answer 1

2

I might be misunderstanding you question, but in Python global variables can be read, but not set, by default. If you want to modify the global variable you need to first declare it as global, then you can set it:

play1 = 1
def some_function():
    global play1
    play1 = #your value here, which will change the global value

If the problem is just that the other module isn't accessing the global variable (which it won't) you can try attaching the variable to the __builtin__ module, so you can access or modify it from any module by just doing:

import __builtin__
__builtin__.play1 = #new value

That's it - it should now be a cross-module global variable.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you clarify what the problem is, then. Where in your code are you modifying play1? Could you provide a more complete version of your code?
got the answer. I did it the other way round.
Got to say that the second way is not a pythonic at all. If you want aplayer to change play1 You should do it with returned value: play1 = aplayer(song,1,0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.