473,322 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
5 2120
dwblas
626 Expert 512MB
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
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
dwblas
626 Expert 512MB
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
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
2,851 Expert Mod 2GB
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

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

Similar topics

1
by: Newgene | last post by:
Hi, group, I have python2.3 installed on win2k. I noticed that when I open a dos format text file (eol is '\r\n'), readline() always returns a line ending with '\n' only, not '\r\n'. While I read...
2
by: ezelasky | last post by:
We are using the bcp utility (via APIs) to export data from a SQL table in a fixed format text file. BCP is inserting spaces for a field if the field contains a NULL. This is fine with us except...
2
by: Jimmy | last post by:
How can i format text, so that user input such as, 01062004 would be formatted as 01/06/2004. I've tried several user-defined number and date formats, but I seem to be missing something. Thank...
3
by: dennist685 | last post by:
Format text as a heading I'm progressing through a walkthrough on DetailsView control Early on it says Type Edit Employees, select the text, and then format the text as a heading. I clicked...
2
by: a | last post by:
keep a list of read and unread items hi guys i m building an rss reader and i want you suggestions for datastructure for keeping read and unread list for each use i m assuming it will be very...
3
by: frank | last post by:
please help! How to keep the format of the content of a MultiLine TextBox. For example, 'new line' I try the code: ------------------------------ TextBox.Text = "TERANET / MPAC - KEY TERMS...
13
by: Neil Cerutti | last post by:
Many of the file formats I have to work with are so-called fixed-format records, where every line in the file is a record, and every field in a record takes up a specific amount of space. For...
12
by: gobblegob | last post by:
Hi guys, This is a very hard question to word. Basically i am opening a .txt file into a label, but sometimes the .txt files are too long and you need to scroll down. So when i open the txt...
2
by: metanarcissus | last post by:
hi i am looking for different values (quantity) in the same column in two tables with same format (same column headings). One is the old version of our db and the other one is the new version. ...
4
by: kkshansid | last post by:
what is the best way to display a word file on a php page in the same format as it is on word format ? word file has some very well diplay of data in tabular format php page has includes...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.