0

我是 python 新手。这是 TXT 文件中的文本行,作业要求我将它们打印在图形窗口上

student1 lastname 55
student4 lastname 55
student9 lastname 55
student10 lastname 55
student12 lastname 55
student15 lastname 55

所以起初我打开文件,进入这个循环并将它们打印在 python shell 上。

file = open("input.txt","r")
    for line in file:
        print(line)

我的图形窗口的代码

from graphics import *   
win = GraphWin('', 600,500)

现在如何在刚刚创建的图形窗口上打印这些文本行?

4

2 回答 2

1

你做得很好,但你应该阅读文档以获得更好的理解。

做你想做的事情的更好方法是

from graphics import *
win = GraphWin('', 600,500)
text_x, text_y = 200, 50
with open("input.txt","r") as file:
    for line in file:
        print(line)
        label = Text(Point(text_x, text_y), line)
        label.draw(win)
        text_y += 50 # distance between 2 lines
于 2013-11-06T01:25:53.600 回答
-1

您可以使用基本列表操作将这些行放入列表中,例如

line = [line.strip() for line in open("file.txt")]

一旦你得到列表并访问for窗口内循环中的每个元素

我建议你通读这个pythongraphics 文档

于 2013-11-05T21:50:54.483 回答