Search code examples
pythonzelle-graphics

Is there a way to add multiple strings to a Text object? (Zelle Graphics)


I'm trying to create a Text object with multiple strings using the Zelle Graphics library, but it seems to accept only a single string argument. This is how I set it up:

text = Text(Point(250, 250), "You have", x, "remaining")

This is the desired output (as displayed in the graphics window):

"You have x remaining"


Solution

  • You neglected to explain that you are using the graphics module by John Zelle. I know that is in the tag, but it is better to be a little bit more explicit. Even experts cannot be expected to know automatically about every library out there.

    The Text class in that module has a constructor with the following signature:

    def __init__(self, p, text): 
    

    meaning you have to pass it a Point object and exactly one string. That means you can't do this:

    text = Text(Point(250, 250), "You have", x, "remaining")
    

    but you can do

    text = Text(Point(250, 250), f"You have {x} remaining")
    

    in Python 3.7 or

    text = Text(Point(250, 250), "You have {x} remaining".format(x=x))
    

    in earlier versions.