0

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")}
netrate
  • 423
  • 2
  • 8
  • 14

1 Answers1

2

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

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Wouldn't this be something that should be updated in the graphics.py file? Are they still updating that? – netrate Mar 09 '22 at 00:27
  • @netrate you would have to write this to author [John Zelle](https://mcsp.wartburg.edu/zelle/). Probably he make updated only when he releases new version of his book [Python Programming: An Introduction to Computer Science](https://mcsp.wartburg.edu/zelle/python/index.html). Last update was `Version 5 8/26/2016` - you can find it in source code. – furas Mar 11 '22 at 04:06