Username: Password:

Perl/TkFAQ-10.18.怎样限制输入框中内容的宽度
来源:linux宝库作者:linux宝库 发布时间:2007-09-30 00:00:00


  原文:

  10.18. How do I limit an Entry’s insertion width?

  [OBSOLETE; explain use of new -validatecommand with 8.0] Nick Ing-Simmons recommends writing a new Entry widget with the insert method appropriately overridden by one that does limit the width. His code is avaialable as a separate package from: http://www.perltk.org/contrib/etc/LEntry-0_00.tar.gz

  Now Brent Powers points out a possible problem with that approach and recommends an insert() method as follows: Date: Thu, 22 Aug 1996 10:32:44 -0400 From: "Brent B. Powers" Subject: Re: How to set max characters for Entry widget In-reply-to: <199608211445.PAA09248@pluto> Ummm, before we set this into the distribution or FAQ, maybe we should make it work properly. An example: Imagine maxwidth configured to 8, the user fills in ABCDEFGH, moves the cursor back 4 places, and types I. The SUPER::insert call sets the string to ABCDIEFGH, which this code then modifies to ABCDIEFG. Hmmm, how about sub insert { my($w, @args) = @_; my($max) = $w->cget(-maxwidth); my($sval) = $w->get; if (length($sval) >= $max) { $w->SUPER::insert(@args); if (length($w->get) > length($sval) { ## Reject it; my($idx) = $w->index(’insert’); # get current cursor position $w->delete(0, ’end’); $w->insert(0, $sval); $w->icursor($idx); $w->bell; } else { $w->SUPER::insert(@args); } } Of course, that still doesn’t deal with the selection, but ...

  To which Nick Ing-Simmons responded (Thu Aug 22 1996): ’paste’ and call insert method, what other selection issues are there?

  译文:

  10.18. 怎样限制输入框中内容的宽度?

  【Perl/Tk的作者Nick Ing-Simmons曾建议重写一个新的Entry组件??适当的重载他的insert方法以便能够限制宽度。】

  下面是Brent Powers为实现此功能提供的一个insert()方法,并且指出了其可能存在的问题:

  日期: Thu, 22 Aug 1996 10:32:44 -0400

  发自: "Brent B. Powers"

  主题: Re: 怎样配置Entry中的最大字符数目?

  In-reply-to: <199608211445.PAA09248@pluto>

  嗯,在我们把下面的代码加入到“常见问题”中去前,也许我们应该先把他修正的更好一些。

  一个例子:假设maxwidth选项已被配置为8,用户先键入了ABCDEFGH,然后移动光标到, moves

  第四个字符后面,键入了I。SUPER::insert的调用将使此字符串变成了ABCDIEFGH,但是下面

  的代码将会把他更正为ABCDIEFG.

  嗯,怎么样?

  sub insert {

  my($w, @args) = @_;

  my($max) = $w->cget(-maxwidth);

  my($sval) = $w->get;

  if (length($sval) >= $max) {

  $w->SUPER::insert(@args);

  if (length($w->get) > length($sval) {

  ## Reject it;

  my($idx) = $w->index(’insert’); # get current cursor position

  $w->delete(0, ’end’);

  $w->insert(0, $sval);

  $w->icursor($idx);

  $w->bell;

  } else {

  $w->SUPER::insert(@args);

  }

  }

  当然,这里还没有考虑字符选择的问题,但是……

  对此,Nick Ing-Simmons作出了回答(Thu Aug 22 1996):

  ’粘贴’和会调用insert方法,此外更有什么其他“选择”的问题呢?

  (译者注:在Tk8.0以后,似乎能够通过配置-validateCommand选项来实现此功能吧。有兴趣的朋友能够自己参考一下Entry的手册:perldoc Tk::Entry)

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅