473,671 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

80 New Member
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 2127
dwblas
626 Recognized Expert Contributor
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
moroccanplaya
80 New Member
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 Recognized Expert Contributor
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
moroccanplaya
80 New Member
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 Recognized Expert Moderator Specialist
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
2748
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 the same file on unix, it returns a line ending with '\r\n' correctly. This makes me difficult to determine the format of a text file, dos or unix. Is this a bug or intended behavior? If not a bug, then how to determine the format of a text...
2
5660
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 at the end of the line, there are no spaces for that field just the end-of-row terminator prematurely, so it looks like that field is not present and messes up another piece of software we pump the text file into down stream. Example -- The...
2
1292
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 you.
3
1675
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 format, then styles, but nowhere found how to make selected text a heading.
2
1986
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 sparse thanks
3
2478
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 OF USE \n" & _ "3.1 \n" & _ .......
13
3235
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 example, one of my older Python programs contains the following to create a fixed-format text record for a batch of new students: new = file("new.dat", "w") if not new:
12
1735
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 file into a textbox it is not in the same format as a label , the label seem's to seperate lines and you can read it easier. any idea's on how to read as the same format as a label Sorry if i havnt explained properly. GobbleGob.
2
1288
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. These were originally excel files that i downloaded into Access as two tables and looked for unmatched records in them. so 2 tables are exactly the same format but there has been some changes in the new table mainly quantity of the orders. i...
4
5470
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 header,footer,left,right i want to display it in the middle but by simply copy pasting error it doesnt display in the same format
0
8474
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8912
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8669
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4403
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.