0
from graphics import *      

win = GraphWin("Hangman", 600, 600)
win.setBackground("yellow")
textEntry = Entry(Point(233,200),10)
textEntry.draw(win)
text = textEntry.getText()
testText = Text(Point(150,15), text)
testText.draw(win)

exitText = Text(Point(200,50), 'Click anywhere to quit')
exitText.draw(win)

win.getMouse()
win.close()

I'm trying to obtain text from the user in Python graphics and be able to use that input, such as manipulate it, search for it in a list, etc. To test that, I created a entry window in graphics and tried to obtain the text from that entry window and simply display it in the window, just to check if it sucessfully obtained the text.

Unfortunately, it isn't working, it just shows the 'Click anywhere to quit' and then the empty window and despite writing text in it it does nothing. What am I doing wrong?

4

1 回答 1

0

以下来自文档

底层事件隐藏在 graphics.py 中的方式,当用户在输入框中输入文本时没有信号。为了向程序发出信号,上面使用了鼠标按下。在这种情况下,鼠标按下的位置无关紧要,但是一旦处理了鼠标按下,就可以继续执行并读取 Entry 文本。

您在绘制条目后立即获得文本,因此它将为空。您需要等待信号,然后读取条目。文档中的这段摘录说等待鼠标单击然后阅读条目。所以尝试添加

    win.getMouse()

到您的代码如下

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)

exitText = Text(Point(200,50), 'Click anywhere to quit')
exitText.draw(win)

win.getMouse()
win.close()

这是输出的样子。注意:我将条目设为 50 宽。 测试修改代码的输出

于 2016-01-05T01:19:59.857 回答