简体   繁体   中英

How would i take all of the data in a text file and put it into a text widget in tkinter + add a scroll bar

How would i take all of the data in a text file and put it into a text widget in tkinter.

Also how would i get that text widget to have a scroll bar

I already have a program that outputs what i need to a .txt document but i am trying to find a good way to read and display that + have a scroll bar

Try something like this:

from Tkinter import *
file = open("filename.txt")
data = file.read()
file.close()

root = Tk()
widget = Text(root)
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
widget.pack(side=LEFT, fill=Y)
scrollbar.config(command=widget.yview)
widget.config(yscrollcommand=scrollbar.set)
widget.insert(END, data)

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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