0

I am trying to create a simple GUI where a user enters some text and I bold certain words. I am using graphics library however my a getting a 'str' object has no attribute 'draw'. Also the window closes nearly instant.

from graphics import *
win = GraphWin("Hangman", 600, 600)
win.setBackground("yellow")
textEntry = Entry(Point(233,200),50)
textEntry.draw(win)

# click the mouse to signal done entering text
win.getMouse()

text = textEntry.getText()
testText = Text(Point(150,15), text)
testText.draw(win)

finalOut = ""

outtxt = text
outtxtSplit = outtxt.split()
for word in outtxtSplit:
    if word == "bold":
        finalOut = finalOut + word.setStyle("bold")
    else:
        finalOut = finalOut + word

outtxt.draw(win)
exitText = Text(Point(200,50), outtxt)
exitText.draw(win)
win.getMouse()
win.close() 
1
  • 1
    outtxt.draw(win) You are calling draw on a str object, the error message tells you that. What behaviour were you expecting? Commented Aug 1, 2017 at 12:58

2 Answers 2

2

Your

outtxt = text

Should be

outtxt = Text(Point(150,15), text)
                      /|\
                       | Put the size you want here.

In your code outtxt is the text text itself, so it has no method called draw()

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to the Point() argument to Text() pointed out in both answer and comment, this line simply won't work:

finalOut = finalOut + word.setStyle("bold")

As finalOut and word are Python str instances and the graphics.py setStyle("bold") method applies to Text() objects.

Fixing this, vs. dropping the feature, could be tricky as you'd need to collect a list of normal and bold Text() instances and draw them in a horizontal row with proper spacing. graphics.py won't help much as I don't see any methods to get formatted text width. It seems that style is all or nothing for the entire text message, not for individual elements of it.

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.