限制 Entry 中的字符数。

huangapple go评论87阅读模式
英文:

How to limit the character in Entry

问题

  1. 我需要将输入限制为最多5位数字
  2. ```python
  3. def validate(S):
  4. try:
  5. float(S)
  6. return True
  7. except ValueError:
  8. messagebox.showerror(message="数据错误,仅限数字。", title="错误")
  9. return False
  10. e1 = tk.Entry(master, validate="key", validatecommand=(master.register(validate), '%S'))

我有这个方法来接收和验证输入,但我想知道如何在输入上设置最多5位数字的限制。

我尝试过使用此问题中使用的方法(https://stackoverflow.com/questions/5446553/tkinter-entry-character-limit),但在validate方法中不起作用。

  1. <details>
  2. <summary>英文:</summary>
  3. I need to limit the entry to a maximum of 5 digits.

def validate(S):
try:
float(S)
return True
except ValueError:
messagebox.showerror(message="Datos erroneos, únicamente números.", title="ERROR")
return False

e1 = tk.Entry(master, validate="key", validatecommand=(master.register(validate), '%S'))

  1. I have this method to receive and validate that it&#39;s just numbers at the entry, but i would like to know how can I put a max limit of 5 digits on the entry.
  2. I tried with the methods used in this question (https://stackoverflow.com/questions/5446553/tkinter-entry-character-limit) but it is not working with the validate method.
  3. </details>
  4. # 答案1
  5. **得分**: 3
  6. validate函数中返回False,如果字符串的长度超过5
  7. ```python
  8. def validate(S):
  9. try:
  10. float(S)
  11. return len(S) <= 5
  12. except ValueError:
  13. messagebox.showerror(message="Datos erroneos, únicamente números.", title="ERROR")
  14. return False
英文:

Return False from validate if the length of the string is more than 5.

  1. def validate(S):
  2. try:
  3. float(S)
  4. return len(S) &lt;= 5
  5. except ValueError:
  6. messagebox.showerror(message=&quot;Datos erroneos, &#250;nicamente n&#250;meros.&quot;, title=&quot;ERROR&quot;)
  7. return False

huangapple
  • 本文由 发表于 2023年2月14日 00:07:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定