如何使用 Python 将结果从文本文件打印到窗口 [英] How to print results from text file to window using Python

查看:192
本文介绍了如何使用 Python 将结果从文本文件打印到窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一堆名称和数字的文本文件,我想将它们打印到我的窗口中.我正在使用 Python 3.3 和 Tkinter.所以详细说明我希望程序读取文本文件,然后在程序中显示文本文件的内容(在标签或文本区域中)

I have a text file with a bunch of names and figures that I would like to print to my window. I am using Python 3.3 and Tkinter. So to elaborate I would like the program to read a the text file and then show the contents of the text file in the program (either in label or text area)

比如:

Results = Label(window, text = "HERE I WANT THE RESULTS FROM THE TEXT FILE")
Results.grid(row = 1, column = 1)

推荐答案

从你的评论看来你知道如何阅读文本文件:

From your comments it seems you know how to read a text file:

data_file = open("myfile.txt")
data = data_file.read()
data_file.close()

并且您知道如何将字符串放入标签中:

And you know how to put a string into a label:

Results = Label(window, text = data)
Results.grid(row = 1, column = 1)

因此,要将它们组合在一起,您只需……将它们组合在一起.

So, to put them together, you just... Put them together.

file = open("myfile.txt")
data = file.read()
file.close()
Results = Label(window, text = data)
Results.grid(row = 1, column = 1)

这篇关于如何使用 Python 将结果从文本文件打印到窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆