kgnickl has asked for the wisdom of the Perl Monks concerning the following question:

I have two things I can't figure out how to do. I'm using the the window to output status of a script that is running. Below I have my questions and the basic line of code I'm using. $detailLog = $top_frame->Scrolled('Text', -scrollbars =>'e', -height=>6,-font=>'Helvetica 8 bold')->pack; 1. How do I disable keyboard input? So a user can't click the window and type stuff in? Not really a big deal I just can't find the answer. 2. How do I clear the existing text? For example the program runs and if the person wants to run again but with differnt parameters (without closing the window) they are stuck with the old output. I'd like to clear that from the window?

Replies are listed 'Best First'.
Re: tK Scrolled Questions?
by Marshall (Canon) on Feb 22, 2011 at 17:43 UTC
    1. There is a Read Only Text widget, 'ROText'. You could use that instead of 'Text'. Does some strange things when you attempt to type in there, but it doesn't let you do it. There are other albeit more complex ways. Try ROText and see if that does it for you.

    2. To clear the text:

    $detailLog->delete("1.0", 'end'); $detailLog->update;
      I could not get the clear window code to work. Just did nothing. Not sure what I'm doing wrong. I called from a function when a button is pressed. $detailLog->delete("1.0", 'end'); $detailLog->update;
        Interesting.. Can you show a simple version of your code?

        Maybe there is something amiss in the button callback? I mean if you are doing something like this:

        $detailLog->insert ('end', $_); # or $detailLog->insert ('end', $string);
        Then,
        $detailLog->delete("1.0", 'end');
        will work...there may be some apparently minor looking syntax error in your code that is causing problems.

        Update: A simple text window with "clearing the text" button is shown below. A couple of other Tk options are also demo'ed. I use 'ROText' instead of 'Text' to show that this is "Read Only". Try it!

        This "enable" and "disable" stuff will usually be ok, but there will be a short interval of time where the user can modify the displayed window.

        I have one application where I just don't care whether or not the user modifies the text window and I don't use ROText. I'm not reading that result and modifying the database. The user can add notes, and formatting characters before doing a "copy and paste" to another application. Fine with me!

        Anyway, what is the problem with $detail_log->delete("1.0", 'end');?

        #!/usr/bin/perl -w use strict; use Tk; use Tk::Frame; #optional - may be needed for an .exe use Tk::ROText; #optional - may be needed for an .exe my $mw = MainWindow->new; my $f_menu_dialog = $mw->Frame()->pack(-side=>'top',-fill=>'x'); $f_menu_dialog->Button( -text =>"Clear All text", -command => \&Clear_all_text, )->pack(-side=>'left'); my $f_text = $mw->Frame( -relief => 'groove', -borderwidth => '4', )->pack ( -expand => 1, -side => 'top', -fill => 'both', ); my $detail_log = $f_text->Scrolled('ROText', -scrollbars => 'e')->pack; foreach ("blah\n", "more blah\n", "yet more blah\n") { $detail_log -> insert('end', $_); } MainLoop(); sub Clear_all_text { $detail_log->delete("1.0", 'end'); }
Re: tK Scrolled Questions?
by stefbv (Priest) on Feb 22, 2011 at 18:54 UTC

    Disable the widget with:

    $detailLog->configure( -state => 'disabled' );

    Update: If disabled, have to enable it first before you can write in it:

    $detailLog->configure( -state => 'normal' ); $detailLog->insert( '1.0', 'some text' ); $detailLog->configure( -state => 'disabled' );

    Stefan

      Worked perfectly! Thanks!