How to read a file onto the text widget 
Author Message
 How to read a file onto the text widget

The title says what I want.

Any hint?
Thanx.
--
+---------------------------------+-------------------------------------------+
! Y. Victor Yang                  ! Graphics, Visualization & Usability Center!

! Lab:    404-894-9634            ! Georgia Institute of Technology           !
!                                 ! Atlanta, Georgia 30332-0280               !
+---------------------------------+-------------------------------------------+



Mon, 04 Mar 1996 04:43:20 GMT  
 How to read a file onto the text widget
If you're really, really lazy, you can do

textwidget delete 1.0 end
textwidget insert end [cat "filename"]

If you want to do it right, here's an example:

# usage: read_text <filename> <textwidget>

proc read_text { filename t } {
  if {! [file exists $filename]} then {
    $t delete 1.0 end
    $t mark set insert 1.0
  } else {
    # should do error checking
    set file [open $filename {r}]
    $t delete 1.0 end
    $t insert end  [read $file]
    $t mark set insert 1.0
    close $file
  }

Quote:
}

--

BU Information Technology; 111 Cummington Street; Boston, MA 02115


Mon, 04 Mar 1996 06:13:26 GMT  
 How to read a file onto the text widget
|> The title says what I want.
|>
|> Any hint?
|> Thanx.

text .t -height 5 -width 80; # plus any other parameters like -wrap & -yscroll
set F [open $filename "r"]
gets $F Line
while {! [eof $F]} {
  .t insert end $Line
  gets $F Line

Quote:
}

close $F

Simple, eh?  Now, if you want scrollbars...

text .t -yscroll ".ysb set" -xscroll ".xsb set" ...
scrollbar .ysb -relief sunken -command ".t yview"
scrollbar .xsb -relief sunken -command ".t xview" -orient horiz

pack append . \
  .ysb {right filly} \
  .xsb {bottom fillx} \
  .t    fill

Hope that helps...
--
  Norm.

+-----------------------------------------------------------------------+
 Norm MacNeil                     Phone: (613) 763-3372
 Data Systems                     Fax:   (613) 765-2854

 #include <disclaimer.std>       "Roller bladers do it in-line!"



Mon, 04 Mar 1996 22:40:18 GMT  
 How to read a file onto the text widget


Quote:
(Norm MacNeil) writes:


   (Yongwei Yang) writes:
   |> The title says what I want.
   |>
   |> Any hint?
   |> Thanx.
   ...
   set F [open $filename "r"]
   gets $F Line
   while {! [eof $F]} {
     .t insert end $Line
     gets $F Line
   }
   close $F

   Simple, eh?  Now, if you want scrollbars...
   ...
   (rest of the useful code in the message deleted)
------------
There is actually an even simpler way to do this.

        set F [open $filename "r"]
        .t insert end [read $F]
        close $F

You should also add error handling to make sure the open file gets
closed if you get an error while reading.

        set F [open $filename "r"]
        if [catch {.t insert end [read $F]} error] {
            # Close the open file and then report original error
            catch {close $F}
            error $error
        }
        close $F

-Sanjay



Mon, 04 Mar 1996 21:46:10 GMT  
 How to read a file onto the text widget

Quote:

>gets $F Line
>while {! [eof $F]} {
>  .t insert end $Line
>  gets $F Line
>}
>close $F

I think a much faster approach, given that you want to read the whole file
witht the same tag (i.e only one font and color for the whole file), is
to do

.t insert end [read $F]
close $F

This reads the whole file into a string and inserts it into the widget at
once (and so you save the time wasting tcl loop you have).

Nadav Har'El.



Wed, 06 Mar 1996 01:07:03 GMT  
 How to read a file onto the text widget

Quote:

> |> The title says what I want.
> |>
> |> Any hint?
> |> Thanx.

> text .t -height 5 -width 80; # plus any other parameters like -wrap & -yscroll
> set F [open $filename "r"]
> gets $F Line
> while {! [eof $F]} {
>   .t insert end $Line
>   gets $F Line
> }
> close $F

> Simple, eh?  Now, if you want scrollbars...

> text .t -yscroll ".ysb set" -xscroll ".xsb set" ...
> scrollbar .ysb -relief sunken -command ".t yview"
> scrollbar .xsb -relief sunken -command ".t xview" -orient horiz

> pack append . \
>   .ysb {right filly} \
>   .xsb {bottom fillx} \
>   .t    fill

> Hope that helps...
> --
>   Norm.

> +-----------------------------------------------------------------------+
>  Norm MacNeil                     Phone: (613) 763-3372
>  Data Systems                     Fax:   (613) 765-2854

>  #include <disclaimer.std>       "Roller bladers do it in-line!"



Mon, 11 Mar 1996 00:32:11 GMT  
 How to read a file onto the text widget

Quote:

>You should also add error handling to make sure the open file gets
>closed if you get an error while reading.
>    set F [open $filename "r"]
>    if [catch {.t insert end [read $F]} error] {
>        # Close the open file and then report original error
>        catch {close $F}
>        error $error
>    }
>    close $F

What about error handling if the file open fails:

if [catch {set F [open $filename]} error] {
        error $error

Quote:
}

--
NAFTA.  Wow.
"It is sad that the governments are chiefed by the double-tongues."
-- Chief Ten Bears


Tue, 12 Mar 1996 08:53:23 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. writing output listing onto screen (or not) and onto file

2. Some questions on elided text in the text widget (and a few other text widget questions)

3. BUG: Cannot select text in read-only text widget / tk8.0p2-Win95

4. How do I read lines of Text from Text File and add them to Array

5. embedding text widgets in other text widgets

6. Reading fortran text files / Parsing ascii files/ Help!!

7. A read-only text view widget

8. Tkinter and read-only text widgets.

9. Redirecting stdout to a read-only text widget

10. Questions about Messagebox and Read only Text Widget

11. how to generate a read-only text widget?

12. reading lines from a text widget

 

 
Powered by phpBB® Forum Software