0

I have some code like this:

#w = open("blabla.py", "w") is already called
w.write("Button(root, text = %s,command=%s).grid(row=%s,column=%s)\n" % textvalue,buttoncommand,str(rowvalue),str(columnvalue))

However, if I run this, I get the following error:

TypeError: not enough arguments for format string

What is wrong?

1
  • yes. They all are assigned to values. Commented May 29, 2014 at 9:19

3 Answers 3

4

Enclose vars into a tuple:

w.write("Button(root, text = %s,command=%s).grid(row=%s,column=%s)\n" % (textvalue,buttoncommand,str(rowvalue),str(columnvalue)))

Or use a better format version:

w.write("Button(root, text = {0},"
      "command={1}).grid(row={2},"
      "column={3})\n".format(textvalue,
                             buttoncommand,
                             str(rowvalue),
                             str(columnvalue)))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I will also try the .format() version also.
0

You need to put the format arguments into a tuple (add parentheses):

... % (textvalue,buttoncommand,str(rowvalue),str(columnvalue))

but the % syntax for formatting strings is becoming outdated. Try this:

"'{0}', '{1}', '{2}', '{3}', '{4}'".format(textvalue,buttoncommand,str(rowvalue),str(columnvalue))

Comments

0

If you pass multiple arguments to a format string you need to put them in brackets:

w.write("Button(root, text = %s,command=%s).grid(row=%s,column=%s)\n" % (textvalue,buttoncommand,str(rowvalue),str(columnvalue)))
                                                                        ^                                                      ^

You also might want to take a look at the new format syntax: https://docs.python.org/2/library/string.html#format-examples

Comments

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.