简体   繁体   English

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

[英]Python 'str' object has no attribute 'draw' when using Zelle graphics.py

I am trying to create a simple GUI where a user enters some text and I bold certain words. 我正在尝试创建一个简单的GUI,用户在其中输入一些文本,然后将某些单词加粗。 I am using graphics library however my a getting a 'str' object has no attribute 'draw'. 我正在使用图形库,但是我的“ str”对象没有属性“ 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() 

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() 在您的代码中, outtxttext 文本本身,因此它没有名为draw()方法

In addition to the Point() argument to Text() pointed out in both answer and comment, this line simply won't work: 除了在答案和注释中都指出了Text()Point()参数外,该行根本行不通:

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

As finalOut and word are Python str instances and the graphics.py setStyle("bold") method applies to Text() objects. 由于finalOutword是Python str实例,所以graphics.py setStyle("bold")方法适用于Text()对象。

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. 修复此问题(而不是删除该功能)可能很棘手,因为您需要收集常规和粗体Text()实例的列表,并以适当的间距将它们绘制在水平行中。 graphics.py won't help much as I don't see any methods to get formatted text width. graphics.py不会有太大帮助,因为我看不到任何获得格式化文本宽度的方法。 It seems that style is all or nothing for the entire text message, not for individual elements of it. 似乎对于整个文本消息而言, 样式是全部或全部,而不是其单个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM