1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import tkinter as tk root = tk.Tk() root.geometry("200x200") sv = tk.StringVar() x = tk.Entry(root, width = 10, textvariable = sv) def CharLim(BeforeWord,AfterWord): return ((AfterWord.isdecimal())and(len(AfterWord)<=4)) or (len(AfterWord)==0) vc = (x.register(CharLim),"%s","%P") x.configure(validate = "key", vcmd = vc) x.pack() root.mainloop() |
実行画面
Entryに数字以外を入力しても入らない
4桁以上入れようとしても入らない