Help with run command + variable.

R. David Murray rdmurray at bitdance.com
Tue Apr 14 21:05:03 EDT 2009


Daniel Holm <d.holmen at gmail.com> wrote:
> Hi everybody,
> 
> I'm creating my first app (SixA @ http://www.launchpad/gsixaxis) and I have
> gtk.combobox to chose a profile. When a profile is chosen I save the
> profiles name as a variable, and now I want to combine a command with the
> variable.
> 
> Lets say that the user choses th profile firefox. This should be exactly
> like running: 'sixa action profile firefox'
> So the command run by my python script should be something like: 'sixa
> action profile $profile'
> Where $profile is the chosen profile variable.
> 
> Here is the code:
>     def on_profile_switch_changed(self, box):
>         model = box.get_model()
>         index = box.get_active()
>         if index:
>             profile = model[index][0]
>             os.system("sixa action profile $profile")
>             prf = pynotify.Notification ("SixA", "Profile changed to
> $profile", "sixa")
>             prf.show ()
> 
> As you can see I also have a notifications on when the profile has been
> changed, which also shows the chosen profile.
> How do I do either of these?

Try googling for 'python string formatting'.  You'll find lot of useful
stuff if you read a bit.  You have three basic choices: traditional
'%' formatting, new-style .format() formatting, or using a Template
string (which would allow you to use the $varname format you used above).
Which you choose depends on how you want to work, but I'd recommend
.format() for most things.

Except for your os.system call, where I'd recommend using the subprocess
module instead, in which case you'd do something like this:

    call(['sixa', 'action', 'profile', profile])

--
R. David Murray             http://www.bitdance.com




More information about the Python-list mailing list