原文:
19. How do I "clear the screen"?
What screen are you trying to clear?
If you are trying to clear a tty (e.g. xterm) screen then try either of the following within your script: system "clear";or print `clear`;
(where the choice between these two depends on the rest of the script: the first is fast - but proceeds via fork and may not occur at exactly the time that you want it to in the script).
David and Rachel Weintraub <davidw@cnj.digex.net> recommend using the old termcap.pl p4 library. You might also consider the perl 5 equivalents: Term.pm (especially the Term::Control module), Curses.pm, Perlmenu.pm, PV.
Returning to X-windows and perl/Tk: if you are trying to eliminate a TopLevel or a MainWindow then try: $main -> destroy;
If you would rather not destroy then try: $main->withdraw; # remove $main->deiconify; # put back
If $w is a sub-window (sub-widget) then $w->pack('forget'); # remove if packed (newer Tk-b9.01++) $w->packForget; # remove if packed (older versions) $w->pack(...); # put back
There are also ways to call low level C-ish versions: $w->UnmapWindow;
but that is for special purposes only....
If you are trying to erase an $item on a Canvas then try: delete($item);
(Thanks to the post by <a904209@pluto.tiuk.ti.com> which extended this answer considerably.)
译文:
19. 如何实现“清屏”操作?
你想要“清”的是什么屏幕呢?
如果你想清除的是一个终端(例如xterm)的屏幕,那么你可以在你的脚本中尝试下面的任何一个方法:
system “clear”;
或者
print `clear`; (译者注:这是针对Unix系统的,Windows系统下可能应该用cls吧!)
这里,对于上面两种方法的选择取决于你脚本下面的内容:第一种方法更快一些,但是它是通过fork来进行的,所以真正的执行的时间可能会不是精确的和程序调用时一致)。
David Rachel Weintraub推荐使用原来Perl4的库termcap.pl。因此你也许可以考虑使用Perl5中相应的库和函数:Term::Cap。(译者注:请使用perldoc Term::Cap来参考手册)
回到X窗口和Perl/Tk的问题来:如果你是想消除掉一个TopLevel或者是MainWindow,那么你可以使用:
$main -> destroy;
如果你并不想完全消毁它们,而只是暂时消失,那么可以用:
$main -> withdraw; #令其消失
$main -> deiconify; #重新出现
如果$w是一个子窗口(子组件),那么需要用:
$w -> packForget; #消除已pack的子组件
$w -> pack(……); #重新再pack回去
当然,也有办法来调用底层的类似C版本的函数:
$w -> UnmapWindow;
但是,这只是用于一些特殊目的而设置的……
如果你是想去除掉一个画布(Canvas)中的一个元件($item),那么可以用:
$canvas -> delete($item);