By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,522 Members | 1,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,522 IT Pros & Developers. It's quick & easy.

A command in a String ?

P: n/a
I have a rather odd question:

Is there a way Python can execute a command within a string ?

For example if I had something like:
_str = "fm=OptionMenu(root,xx,'string1','string2','string 3')"

And the execute _str.

The reason why I am trying to do this that as I am aware the OptionMenu
hasn't the ability insert options therefore the options would be inserted
by manipulating strings.

Pete

Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Peter Moscatt wrote:
I have a rather odd question:

Is there a way Python can execute a command within a string ?

For example if I had something like:
_str = "fm=OptionMenu(root,xx,'string1','string2','string 3')"

And the execute _str.
exec _str
The reason why I am trying to do this that as I am aware the OptionMenu
hasn't the ability insert options therefore the options would be inserted
by manipulating strings.


does this do what you want?

strings = "string1", "string2", "string3"
fm = OptionMenu(root, xx, *strings)

</F>

Jul 18 '05 #2

P: n/a
Peter Moscatt wrote:
I have a rather odd question:

Is there a way Python can execute a command within a string ?

For example if I had something like:
_str = "fm=OptionMenu(root,xx,'string1','string2','string 3')"

And the execute _str.

The reason why I am trying to do this that as I am aware the OptionMenu
hasn't the ability insert options therefore the options would be inserted
by manipulating strings.

Pete


Look into the "eval" operation ...

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jul 18 '05 #3

P: n/a
</F>,

Thanks for the help on this. Yea the 'exec' did the trick.
The second example just placed all the strings in one option button.

Pete

On Wed, 13 Apr 2005 10:34:20 +0200, Fredrik Lundh wrote:
Peter Moscatt wrote:
I have a rather odd question:

Is there a way Python can execute a command within a string ?

For example if I had something like:
_str = "fm=OptionMenu(root,xx,'string1','string2','string 3')"

And the execute _str.


exec _str
The reason why I am trying to do this that as I am aware the OptionMenu
hasn't the ability insert options therefore the options would be inserted
by manipulating strings.


does this do what you want?

strings = "string1", "string2", "string3"
fm = OptionMenu(root, xx, *strings)

</F>


Jul 18 '05 #4

P: n/a
Peter Moscatt wrote:
Thanks for the help on this.**Yea*the*'exec'*did*the*trick.
The second example just placed all the strings in one option button.


Peter, you most certainly do *not* need an exec statement. If you take the
time to explain what you want to do (as opposed to how you want to do it) a
bit more exhaustively, perhaps with some context code, the profit will be
all on your side.

Peter

Jul 18 '05 #5

P: n/a
Peter Moscatt wrote:
strings = "string1", "string2", "string3"
fm = OptionMenu(root, xx, *strings)


Thanks for the help on this. Yea the 'exec' did the trick.
The second example just placed all the strings in one option button.


did you include the asterisk? if you remove that, you get one option. if you
include it, you get three options.

</F>

Jul 18 '05 #6

P: n/a
</F>,

No, I hadn't included the '*' Ya learn something every day.

Pete
On Wed, 13 Apr 2005 11:07:13 +0200, Fredrik Lundh wrote:
Peter Moscatt wrote:
strings = "string1", "string2", "string3"
fm = OptionMenu(root, xx, *strings)


Thanks for the help on this. Yea the 'exec' did the trick.
The second example just placed all the strings in one option button.


did you include the asterisk? if you remove that, you get one option. if you
include it, you get three options.

</F>


Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.