Tkinter输入多重验证

问题描述 投票:0回答:1

我正在尝试验证键]的焦点,以便将输入限制为允许的字符并且当用户集中精力(留下有效条目)时,我可以调用一个函数。

像这样可能吗?

import tkinter as tk


class Example(tk.Frame):

def __init__(self, parent):
    tk.Frame.__init__(self, parent)

    vcmd = (self.register(self.on_validate), '%S')
    vcmd2 = (self.register(self.if_valid), '%s')

    # This is the meat of the question; looking to do something like
    self.entry = tk.Entry(self, 
                          validate="key", 
                          validate2="focusout",
                          validatecommand=vcmd, 
                          validate2command=vcmd2)

    self.text = tk.Text(self, height=10, width=40)
    self.entry.pack(side="top", fill="x")
    self.text.pack(side="bottom", fill="both", expand=True)

    def on_validate(self, S):

        # Disallow anything but binary string
        if S == '1' or '0':
            return True
        else:
            self.bell()
            return False

      def if_valid(self, s):
          if len(s) == 3:
             self.my_function(s)
             return True
          else:
             return False

     def my_function(self, value):
        # send value somewhere
        pass

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

我正在尝试对键和焦点进行验证,以使输入仅限于允许的字符,并且当用户进行焦点对准(保留有效输入)时,我可以调用函数。就像...

python validation tkinter textbox
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.