467,926 Members | 1,724 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

non blocked read in tk

Hi!

I tryed this script to non blocked read in tk

Expand|Select|Wrap|Line Numbers
  1. use Tk;                        
  2. use Term::ReadKey;
  3.  
  4. ReadMode 4, STDIN;
  5. $in = \*STDIN;
  6.  
  7. $mw=MainWindow->new();    
  8. $mw->maxsize(500,800);                                
  9. $mw->minsize(500,800);
  10.  
  11. $text=$mw->Text( -width=>65,            
  12.                  -height=>5,
  13.                 );
  14.     $text->place(-x => 20,-y => 700);
  15. $text->focus();
  16.  
  17. $g2 = $mw->Button(-text => "Button",                    
  18.                  -command => sub{ &rutin}
  19.                             );
  20. $g2->place(-x => 240, -y => 100);
  21.  
  22. Mainloop();
  23.  
  24. sub rutin
  25.        {
  26.             while(1)
  27.                   {
  28.                        $k = "";
  29.                 $k=ReadKey(-1, $in);
  30.                     if($k ne "")
  31.                         {
  32.                             $text->insert('insert',$k);
  33.                             $text->focus();
  34.                             last;
  35.                         }
  36.                   }
  37.  
  38.        }

It doesnt work just if I change to the prompt shell and there hit a key.
Have anybody idea how possible take non blocked read whithout this

WIN XP, Active Perl 5.10
Oct 23 '08 #1
  • viewed: 1583
Share:
9 Replies
Icecrack
Expert 100+
Hi!

I tryed this script to non blocked read in tk

Expand|Select|Wrap|Line Numbers
  1. use Tk;                        
  2. use Term::ReadKey;
  3.  
  4. ReadMode 4, STDIN;
  5. $in = \*STDIN;
  6.  
  7. $mw=MainWindow->new();    
  8. $mw->maxsize(500,800);                                
  9. $mw->minsize(500,800);
  10.  
  11. $text=$mw->Text( -width=>65,            
  12.                  -height=>5,
  13.                 );
  14.     $text->place(-x => 20,-y => 700);
  15. $text->focus();
  16.  
  17. $g2 = $mw->Button(-text => "Button",                    
  18.                  -command => sub{ &rutin}
  19.                             );
  20. $g2->place(-x => 240, -y => 100);
  21.  
  22. Mainloop();
  23.  
  24. sub rutin
  25.        {
  26.             while(1)
  27.                   {
  28.                        $k = "";
  29.                 $k=ReadKey(-1, $in);
  30.                     if($k ne "")
  31.                         {
  32.                             $text->insert('insert',$k);
  33.                             $text->focus();
  34.                             last;
  35.                         }
  36.                   }
  37.  
  38.        }

It doesnt work just if I change to the prompt shell and there hit a key.
Have anybody idea how possible take non blocked read whithout this

WIN XP, Active Perl 5.10
Change This:

Expand|Select|Wrap|Line Numbers
  1. $g2 = $mw->Button(-text => "Button",                    
  2.                  -command => sub{ &rutin}
  3.                             );
to:
Expand|Select|Wrap|Line Numbers
  1. $g2 = $mw->Button(-text => "Button",                    
  2.                  -command => sub \&rutin
  3.                             );
Oct 23 '08 #2
KevinADC
Expert 2GB
You can't say "sub \&rutin". "sub" needs brackets

Expand|Select|Wrap|Line Numbers
  1. sub {anonymous function};
  2.  
  3. sub named_function {
  4. ....
  5. }
So this will work:

Expand|Select|Wrap|Line Numbers
  1. $command =  sub {&foo};
  2. $command->();
  3. sub foo {
  4.    print "here we are";
  5. }
  6.  
and this will work the same

Expand|Select|Wrap|Line Numbers
  1. $command =  \&foo;
  2. $command->();
  3. sub foo {
  4.    print "here we are";
  5. }
Oct 23 '08 #3
Icecrack
Expert 100+
My Bad, I made a mistake it was meant to be
Expand|Select|Wrap|Line Numbers
  1. $g2 = $mw->Button(-text => "Button",                    
  2.                  -command => \&rutin
  3.                             );
Thank you for picking that up kevin
Oct 23 '08 #4
KevinADC
Expert 2GB
My Bad, I made a mistake it was meant to be
Expand|Select|Wrap|Line Numbers
  1. $g2 = $mw->Button(-text => "Button",                    
  2.                  -command => \&rutin
  3.                             );
Thank you for picking that up kevin
"Experts" are allowed two mistakes a year otherwise they get demoted to "Amatuer". ;)
Oct 24 '08 #5
Icecrack
Expert 100+
Also Mainloop(); is wrong

Case its meant to be MainLoop(); with the upper case L in Loop.

Expand|Select|Wrap|Line Numbers
  1. MainLoop();
Oct 24 '08 #6
Icecrack
Expert 100+
"Experts" are allowed two mistakes a year otherwise they get demoted to "Amatuer". ;)

at least i found the problem :P
Oct 24 '08 #8
Icecrack
Expert 100+
Anyways.....

http://www.tek-tips.com/viewthread.c...1508918&page=1

i guess he found his answer.
Oct 24 '08 #9
i guess he found his answer.
No im not, my problem is different:
http://www.tek-tips.com/viewthread.c...1508918&page=1

Any idea?

Thanks!
Oct 24 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Jay Vinton | last post: by
2 posts views Thread by jfradette | last post: by
17 posts views Thread by laurenq uantrell | last post: by
1 post views Thread by Yim | last post: by
4 posts views Thread by Dr. J | last post: by
2 posts views Thread by Qindong Zhang | last post: by
4 posts views Thread by dd | last post: by
5 posts views Thread by Damien582 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.