官术网_书友最值得收藏!

How it works...

With the validate option set to "key", we will activate the entry validation that gets triggered on any content modification. The value is "none" by default, which means that there is no validation.

Other possible values are "focusin" and "focusout", which validate when the widget gets or loses the focus, respectively, or simply "focus" to validate in both cases. Alternatively, we can use the "all" value to validate in all situations.

The validatecommand function is called each time the validation is triggered, and it should return true if the new content is valid, and false otherwise.

Since we need more information to determine whether the content is valid or not, we create a Tcl wrapper around our Python function using the register method of the Widget class. Then, you can add the percent substitution for each parameter that will be passed to the Python function. Finally, we will group these values as a Python tuple. This corresponds to the following line from our example:

vcmd = (self.register(self.validate_username), "%i", "%P") 

In general, you can use any of the following substitutions:

  • %d: Type of action; 1 for insertion, 0 for deletion, and -1 otherwise
  • %i: Index of the string being inserted or deleted
  • %P: Value of the entry if the modification is allowed
  • %s: Value of the entry before the modification
  • %S: String content that is being inserted or deleted
  • %v: The type of validation currently set
  • %V: Type of validation that triggered the action
  • %W: The name of the Entry widget

The invalidcommand option takes a function that is invoked when validatecommand returns false. The same percent substitutions can be applied to this option, but in our example, we directly passed the print_error() method of our class.

主站蜘蛛池模板: 通道| 阿克陶县| 怀柔区| 齐齐哈尔市| 重庆市| 乡城县| 石嘴山市| 唐山市| 青川县| 阳谷县| 五大连池市| 南投市| 荆门市| 读书| 上蔡县| 安国市| 三亚市| 苗栗县| 常熟市| 宜良县| 永清县| 内乡县| 林周县| 政和县| 易门县| 图木舒克市| 襄樊市| 阳曲县| 滁州市| 九台市| 泗水县| 玉林市| 迁安市| 桦南县| 鄂伦春自治旗| 宜宾县| 金堂县| 桃江县| 青龙| 吉隆县| 夹江县|