Zelle graphics.py and justify - is there a way to justify text?

278 Views Asked by At

I was looking through the graphics.py file and noticed two instances of justify, but I have never found any instructions on how to use it. All text seemed to be centered when used, I wanted to be able to justify left and right. Maybe I missed it somewhere. Any ideas if there is a command for it?

class Text(GraphicsObject):
    
    def __init__(self, p, text):
        GraphicsObject.__init__(self, ["justify","fill","text","font"])
        self.setText(text)
        self.anchor = p.clone()
        self.setFill(DEFAULT_CONFIG['outline'])
        self.setOutline = self.setFill

and

# Default values for various item configuration options. Only a subset of
#   keys may be present in the configuration dictionary for a given item
DEFAULT_CONFIG = {"fill":"",
      "outline":"black",
      "width":"1",
      "arrow":"none",
      "text":"",
      "justify":"center",
                  "font": ("helvetica", 12, "normal")}
1

There are 1 best solutions below

2
On

The Text class appears to be missing the setJustify() method. We can define it and simply call it on our Text instance:

from graphics import *

def setJustify(self, option):
    if not option in ["left", "center", "right"]:
        raise GraphicsError(BAD_OPTION)
    self._reconfig("justify", option)

win = GraphWin()

t = Text(Point(120, 15), "Right Justified Text\n1 2 3")

setJustify(t, 'right')

t.draw(win)

win.getMouse()
win.close()

enter image description here