How to print results from text file to window using Python [closed]

我的未来我决定 提交于 2021-02-08 12:13:35

问题


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)

So something like:

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

回答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)



回答2:


Here's what you need to do to update a text label after updating:

Results.config(text="New Text")
Results.update_idletasks()

You can also run the update_idletasks() function against the main Tkinter window object, which will refresh all values that have been updated.

As far as opening a file and reading values from it, check out this page:

http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/File_IO



来源:https://stackoverflow.com/questions/23889170/how-to-print-results-from-text-file-to-window-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!