- Tkinter GUI Application Development Cookbook
- Alejandro Rodas de Paz
- 289字
- 2021-08-27 19:44:02
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.
- Java深入解析:透析Java本質的36個話題
- Visual Basic程序設計實驗指導(第4版)
- MySQL數據庫管理與開發(慕課版)
- SQL Server 2012數據庫管理與開發項目教程
- Mastering Apache Spark 2.x(Second Edition)
- FPGA嵌入式項目開發實戰
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- Python 3 Object:oriented Programming(Second Edition)
- TypeScript圖形渲染實戰:2D架構設計與實現
- 精通Spring:Java Web開發與Spring Boot高級功能
- JSP應用與開發技術(第3版)
- 新手學ASP.NET 3.5網絡開發
- 趣味掌控板編程
- JavaScript程序設計實例教程(第2版)
- Mastering Responsive Web Design