in reply to how to display output of a process in text widget in real time

My objective is to turn my "frame" into a text widget as soon as the button is pressed so that in the text widget I can display the output of the command line command (another process).

"turning a frame into a text widget" makes no sense. A frame is something that you would put a text widget into. You might want to make a new text widget appear inside of a frame or not. I doubt that you would want to destroy that frame.

Below is a simple Tk application. Make and copy the testing123.pl code into its own file. Run main program. Some kind of event like clicking a button is needed to start the whole process. The testing123.pl program randomly puts a little delay so that you can see that the lines are being displayed in realtime. The key to that is that a screen update method is called to "refresh" the screen after every line is added.

Update: I noticed that if you press GoButton again, it adds yet another text widget underneath the first one. I guess that is a "feature" of this demo!

#!/usr/bin/perl -w use strict; use Tk; my $mw = MainWindow->new; my $main_frame = $mw->Frame()->pack(-side=>'top',-fill=>'x'); my $go_button = $main_frame->Button(-text=>'Go Button', -command => \&do_command)->pack(); MainLoop; sub do_command { # create and display text widget my $tw = $main_frame->Text()->pack(); open (COPY_PROJ, '-|', 'perl testing123.pl') or die "unable to star +t $!"; my $first_line = "Copying project...please wait"; $tw->insert( 'end', $first_line ); my $copy_proj_line; while (defined ($copy_proj_line =<COPY_PROJ>) ) { $tw->insert( 'end', $copy_proj_line ); $tw->update(); ### this is the key to see values as they come } } __END__ file: testing123.pl: #!/usr/bin/perl -w use strict; $|=1; #no buffering on stdout for (1..10) { print "line number $_\n"; sleep 1 if (rand() > 0.5); }
  • Comment on Re: how to display output of a process in text widget in real time
  • Download Code

Replies are listed 'Best First'.
Re^2: how to display output of a process in text widget in real time
by perlnu (Initiate) on Jul 12, 2011 at 17:56 UTC
    Thanks Marshall, By using update() I can now get the output to show up in the text widget... The one problem which I am still not sure how to solve is, the process I mention is invoked by hitting the button...this button stays depressed for like 5 seconds or so...what's the trick to un-depress this button as soon as it's clicked...?
      Great to hear that you've made progress! I don't have much to go on here as you haven't shown your button code. My button "works" so you will have to figure out what is different between what you are doing and what I did.

      Try to boil the problem down to the simplest set of code possible that still illustrates the problem. Sometimes that process itself results in some enlightenment. If it doesn't, post the code here and we'll look at it. Without seeing the code, it would just be guessing on my part.

        Thanks again Marshall - the piece of the code is included below. In the meantime I am trying to figure this out as well...
        sub copy_project { # first check for null for Project Name # bail out if Project Name is null if ( !defined $open_source_project_file ) { print "Select Source Project file (.cpm)\n"; exit(); } # check to ensure that project location # is selected by user if ( !defined $destination_project_loc ) { print "Destination project location not specified!\n"; exit(); } # check to ensure that destination project # folder is specified by user if ( !defined $destination_proj_folder ) { print "Destination Project Folder not specified!\n"; exit(); } else { # check for spaces in project name &check_destination_folder_for_spaces; # check to ensure underscore in project name &check_destination_for_underscores; } # check to ensure that directory by the # name of Project Name does not already # exist at Project Location # Following three cases to be considered: # 1. Directory exists but is empty # 2. Directory exists, and not empty, bail out, and ask user for new p +roject name # 3. Directory does not exist if ( -d $destination_project_loc . "/" . $destination_proj_folder +) { # check to ensure this dir. is empty opendir( DIRH, $destination_project_loc . "/" . $destination_proj_folder +); my @dest_proj_folder_files = grep { $_ ne '.' and $_ ne '..' } readdir(DIRH); closedir(DIRH); if (@dest_proj_folder_files) { my $project_dir_exists_error = $mw->messageBox( -title => "Error in Project Name", -message => 'Project Name directory exists, and is non + empty!', -type => 'RetryCancel', -icon => 'error', ); if ( $project_dir_exists_error eq "Retry" ) { &initialize_all_variables; } else { exit(); } } else { # Project Name directory exists but is empty my $final_copy_project_command = &create_copy_project_comm +and; # get rid of top_frame $top_frame->packForget(); # create and display text widget my $tw = $second_frame->Text()->pack(); my $first_line = "Copying project...please wait"; $tw->insert( 'end', $first_line ); my @read_copy_project_command_output = `$final_copy_project_command`; foreach my $output_line (@read_copy_project_command_output +) { $tw->insert( 'end', $output_line ); } } } else { # Project Name directory does not exist # get rid of top_frame $top_frame->packForget(); # create and display text widget #my $tw = $second_frame->Text()->pack(); my $tw = $second_frame->Scrolled("Text")->pack(); my $first_line = "Copying project...please wait\n"; $tw->insert( 'end', $first_line ); my $final_copy_project_command = &create_copy_project_command; #open(COPY_PROJ, "$final_copy_project_command|"); open( COPY_PROJ, '-|', "$final_copy_project_command" ); my $copy_proj_line; while ( defined( $copy_proj_line = <COPY_PROJ> ) ) { $tw->insert( 'end', $copy_proj_line ); $tw->update(); $tw->see('end'); } } } # This sub is called by copy_project sub create_copy_project_command { # prepare copy_project_command # location of destination project depends on # whether same name project dir exists or not my $location_destination_proj; if ( -d $destination_project_loc . "/" . $destination_proj_folder +) { $location_destination_proj = $destination_project_loc; } elsif ( !-d $destination_project_loc . "/" . $destination_proj_fol +der ) { $location_destination_proj = $destination_project_loc . "/" . $destination_proj_folder; } # new project name is derived from what # the user types in Project Name text box my $new_project_name = $destination_proj_folder; my $new_project_file_name = $new_project_name . "\.cpm"; my $new_design_library_name = $new_project_name . "_lib"; # Design name is same as project name my $new_design_name = $new_project_name; my $license = "Concept_HDL_expert"; my $copy_project_command = "copyproject -proj " . "\"" . $open_source_project_file . "\"" . " \-copytopath " . "\"" . $location_destination_proj . "\"" . " \-newprojname " . "\"" . $new_project_file_name . "\"" . " \-newlib " . "\"" . $new_design_library_name . "\"" . " \-newdesign " . "\"" . $new_design_name . "\"" . " \-product " . "\"" . $license . "\""; return $copy_project_command; }