使用Zelle graphics.py时,Python'str'对象没有属性'draw'

时间:2017-08-01 12:55:41

标签: python python-3.x zelle-graphics

我正在尝试创建一个简单的GUI,用户输入一些文本,然后我加粗某些单词。我正在使用图形库但是我得到一个'str'对象没有属性'draw'。窗口几乎瞬间关闭。

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() 

2 个答案:

答案 0 :(得分:2)

outtxt = text

应该是

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

在您的代码中outtxttext 文本本身,因此它没有名为draw()的方法

答案 1 :(得分:0)

除了答案和评论中指出的Point() Text()参数外,此行根本不起作用:

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

由于finalOutword是Python str个实例,并且graphics.py setStyle("bold")方法适用于Text()个对象。

修复此问题,与删除功能相比,可能会非常棘手,因为您需要收集正常和粗体Text()实例的列表,并将它们绘制在具有适当间距的水平行中。 graphics.py没有多大帮助,因为我没有看到任何获取格式化文本宽度的方法。似乎 style 对于整个短信都是全部或全部,而不是单个元素。

相关问题