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

I would like to create a wrapper for Windows cmd.exe (to feed cmd.exe with input from and display its output in a Tk application). However if non ASCII characters generated by cmd.exe are fed into a Tk text widget the result is garbage. Applying utf8 decoding results in '?' characters appearing in the place of none ASCII characters (which is slightly better). Any idea as to what decoding to apply to these characters to appear correctly in a Tk text widget?
  • Comment on Windows cmd.exe output -> Tk text widget?

Replies are listed 'Best First'.
Re: Windows cmd.exe output -> Tk text widget?
by zentara (Cardinal) on Feb 10, 2012 at 09:31 UTC
    My question is still: how to convert cmd.exe output into Tk text widget readable format?

    Post a small working example, so the Windows users can try and help. Don't expect us to write your example code.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      use Tk; use IPC::Open3; use IO::Handle; use Win32::Process::Kill; use threads; use threads::shared; use Thread::Queue; $q=Thread::Queue->new; $qe=Thread::Queue->new; unlink('out.txt'); my %font=(-font=>[-family=>'Courier New',-size=>10]); my %width=(-width=>120); $pid = open3( \*CHILD_IN, \*CHILD_OUT, \*CHILD_ERR, 'cmd.exe' ); print "pid = $pid\n"; sleep(1); threads->create(\&handle_child_out)->detach; threads->create(\&handle_child_err)->detach; print "threads created\n"; autoflush CHILD_OUT; autoflush CHILD_ERR; my %font=(-font=>[-family=>'Courier New',-size=>10]); my %width=(-width=>118); my $mw=new MainWindow; my $entry=$mw->Entry( %width, -relief=>'groove', -borderwidth=>5, -textvariable=>\$entry_text, %font )->pack; $entry->focus; $entry->bind('<Key-Return>',\&enter); my $text=$mw->Scrolled('Text', -insertontime=>0, -scrollbars=>'e', %font, %width, -height=>43 )->pack; my $geom=$mw->geometry; $geom='+5+5'; $mw->geometry($geom); $mw->after(500,\&handler); MainLoop; Kill($pid); sub enter { print CHILD_IN "$entry_text\n"; $entry_text=''; $entry->focus; } sub handler { while(($q->pending)||($qe->pending)) { my ($content,$contente); if($q->pending) { $content=$q->dequeue; $content=~s/\r//g; } if($qe->pending) { $contente=$qe->dequeue; $contente=~s/\r//g; } my ($x,$y)=$text->yview(); $text->insert('1.0',"$content$contente"); $text->yview(moveto=>$x); $entry->focus; } $mw->after(500,\&handler); } sub handle_child_out { sleep(1); do { sysread CHILD_OUT, $content, 4092; if($content ne '') { $q->enqueue($content); } } while(1); } sub handle_child_err { sleep(1); do { sysread CHILD_ERR, $content, 4092; if($content ne '') { $q->enqueue($content); } } while(1); }
Re: Windows cmd.exe output -> Tk text widget?
by Anonymous Monk on Feb 10, 2012 at 07:44 UTC
    • help: lists the available dos commands - not helpful
    • help cmd: if I use open3 with 'cmd.exe /u' or 'cmd.exe','/u' (for unicode output) it fails
    • chcp: what do I do with the number of the code page in a Tk text widget?
    • Proc::Background: this part of the problem has been solved already

    • My question is still: how to convert cmd.exe output into Tk text widget readable format?