By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,760 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,760 IT Pros & Developers. It's quick & easy.

how to keep the same format for text that has been read

P: 80
hi i have i open a url using urllib.request.urlopen then store the source code by using url.read to a variable and i paste the source code into the tkinter text widget but how do you keep the same format ??

Expand|Select|Wrap|Line Numbers
  1.  
  2. with urllib.request.urlopen(path) as url:
  3.         sourcecode = url.read()
  4.         global storecode
  5.         storecode = sourcecode
  6.         tkinter.messagebox.showinfo("sourcode copied:", path)
  7.         Text.insert(tkinter.END,storecode) 
  8. #i want the source  code to be kept in he same format
  9.  
Jan 21 '12 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 626
One way is to use readlines() and insert each line individually if newlines are not displaying properly. But we do not know what is wrong with the display you get, and we do not know what type of widget "Text" is. I would suggest that you take a look at the Pmw extension, especially the ScrolledText widget.
Jan 21 '12 #2

P: 80
im using the standard tkinter.Text widget

i sorted it out with readlines() but im guessing the text widget is not wide enough and i need to add a horizontal scroll bar

is the scrolledtext widget better ?
Jan 21 '12 #3

Expert 100+
P: 626
Scrollbars are not that difficult. You just attach them to the xview or yview of the widget (an example for the Text widget is at http://effbot.org/zone/tkinter-scrollbar-patterns.htm), but why re-invent the wheel?
Jan 21 '12 #4

P: 80
thanks i have the scroll bars on, but the size of the scroll bar is really small its not the same size as the tkinter text widget, i tried putting the scroll bar in a separate frame but that did not work, how do you set the length of the scroll bar is thee an option for it?
Jan 23 '12 #5

bvdet
Expert Mod 2.5K+
P: 2,851
HTML was designed for data display with a focus on how it looks. It won't display the same in a Tkinter Text widget. Here's an example of a Tkinter Text widget with scrollbars that displays the contents of a text file:
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. """
  3. Edit a file and save the text.
  4. """
  5.  
  6. textFont1 = ("Courier New", 16, "normal")
  7.  
  8. class ScrollbarX(Tkinter.Scrollbar):
  9.     def set(self, low, high):
  10.         if float(low) <= 0.0 and float(high) >= 1.0:
  11.             self.grid_remove()
  12.         else:
  13.             self.grid()
  14.         Tkinter.Scrollbar.set(self, low, high)
  15.  
  16. class App(Tkinter.Tk):
  17.     def __init__(self, fn, fnout):
  18.         Tkinter.Tk.__init__(self)
  19.         self.title("Text Widget")
  20.         self.fin = open(fn, 'r')
  21.         self.fnout = fnout
  22.         self.mainFrame = Tkinter.Frame(self)
  23.         self.mainFrame.grid()
  24.         self.exit = Tkinter.Button(self.mainFrame,
  25.                                    text="Save and Exit",
  26.                                    command=self.finish)
  27.         self.exit.grid(row=0, column=0, sticky="ns")
  28.  
  29.         vscrollbar = ScrollbarX(self.mainFrame)
  30.         vscrollbar.grid(row=1, column=1, sticky="ns")
  31.         hscrollbar = ScrollbarX(self.mainFrame, orient=Tkinter.HORIZONTAL)
  32.         hscrollbar.grid(row=2, column=0, sticky="ew")
  33.  
  34.         self.textWidget = Tkinter.Text(self.mainFrame,
  35.                                        yscrollcommand=vscrollbar.set,
  36.                                        xscrollcommand=hscrollbar.set,
  37.                                        wrap=Tkinter.NONE,
  38.                                        height=24,
  39.                                        width=60,
  40.                                        font=textFont1)
  41.         self.textWidget.insert("1.0", self.fin.read())
  42.         self.textWidget.grid(row=1, column=0, sticky="nsew")
  43.  
  44.         hscrollbar["command"] = self.textWidget.xview
  45.         vscrollbar["command"] = self.textWidget.yview
  46.  
  47.     def finish(self):
  48.         fout = open(self.fnout, 'w')
  49.         fout.write(self.textWidget.get("1.0", "end"))
  50.         fout.close()
  51.         self.fin.close()
  52.         self.quit()
  53.  
  54. if __name__ == "__main__":
  55.     fn = "edit.txt"
  56.     fnout = "editresult.txt"
  57.     app = App(fn, fnout)
  58.     app.mainloop()
  59.     app.destroy()
Jan 23 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.