0

I'm working a tkinter application, i want to display a read only text file, whenever i add the text file using label or text, the file is not oranised at all

I want to open a text file in Label or Text(which ever works), but what I keep getting is a text that skids beyond my window frame. I added the scroll button, it's still doing the same thing. I want to the text to be well ordered in a specified Label/Text widgets(read only). thank you in advance.

from tkinter import *
root = Tk()

text_file = open("C:\\Users\stone's\Desktop\\works.txt")
text1 = text_file.read()
for i in text1:
       if len(text1)==50:
               ## MOVE TO NEXT LINE
                Label(root, text="%s" % ('\n'), 
                      font=('Bradley Hand ITC', '25', 'bold'), 
bg='#c9e3c1').pack()
       else:
               ## DON'T MOVE OVER TO NEXT LINE
                Label(root, text="%s" % (i), font=('Bradley Hand ITC', 
'25', 'bold'
                                                   ), 
bg='#c9e3c1').pack(side = LEFT)
        ## ALL I'M TRYING TO DO IS TO SHOW A TEXT ON A LABEL APPROPRIATELY
       ## WITHOUT THE TEXTS SKIDDING OUT OF THE WINDOW FRAME
root.mainloop()
lvingstone
  • 171
  • 8

1 Answers1

0

As said in the answers in this question the question is for python 2 however the comment I linked shows it for python 3 so you can make the text widget readonly.

As a side note: labels are for displaying small pieces of text as labels for other elements not displaying a whole file read only.

CodeAlkemist
  • 11
  • 1
  • 4
  • But even with Text widget I'm still faced with the same problem. – lvingstone Jan 17 '19 at 20:45
  • To avoid unnecessary repetition of long answers you should be able to attach a scrollbar to the text widget as per this answer https://stackoverflow.com/questions/13832720/how-to-attach-a-scrollbar-to-a-text-widget – CodeAlkemist Jan 17 '19 at 20:48
  • What I don't want to add is a horizontal scrollbar. My text continues horizontally and that is where my problem is coming from – lvingstone Jan 17 '19 at 21:04
  • I did overlook the fact that for the text widget to display a new line a '\n' character must be present so you can try adding one of those every n characters a resource on the text widget: http://effbot.org/tkinterbook/text.htm – CodeAlkemist Jan 17 '19 at 21:36