当使用focusOut验证TKINTER中的条目时,如何在焦点上验证下一个条目验证方法不触发

发布于 2025-01-18 12:42:57 字数 2412 浏览 3 评论 0原文

import tkinter
from tkinter import *
   
def validate_name(name):
    if len(name) > 2 and not name.isdigit():
        name_textBox.config(bg='#C4C4C4')      
        return True
    elif any(ch.isdigit() for ch in name):
        name_textBox.config(bg='#F56B6F')                
        name_textBox.delete(0, END)  
        name_textBox.focus_set() 
        return False  
    elif len(name) == 0:                    
        name_textBox.config(bg='#F56B6F')
        name_textBox.delete(0, END)  
        name_textBox.focus_set() 
        return False
    elif len(name) <= 2:
        name_textBox.config(bg='#F56B6F')
        name_textBox.delete(0, END)  
        name_textBox.focus_set() 
        return False
    else:                  
        name_textBox.config(bg='#F56B6F') 
        name_textBox.delete(0, END)  
        name_textBox.focus_set() 
        return False                  
                      
def validate_lname(lname):     
    if len(lname) > 2 and not lname.isdigit():
        lname_textBox.config(bg='#FFFFFF')      
        return True
    elif any(ch.isdigit() for ch in lname):
        lname_textBox.config(bg='#F56B6F')                
        lname_textBox.delete(0, END)  
        lname_textBox.focus_set() 
        return False  
    elif len(lname) == 0:                    
        lname_textBox.config(bg='#F56B6F')
        lname_textBox.delete(0, END)  
        lname_textBox.focus_set() 
        return False
    elif len(lname) <= 2:
        lname_textBox.config(bg='#F56B6F')
        lname_textBox.delete(0, END)  
        lname_textBox.focus_set() 
        return False
    else:                  
        lname_textBox.config(bg='#F56B6F')                
        lname_textBox.delete(0, END)  
        lname_textBox.focus_set() 
        return False   

                      

root = Tk()  
name_textBox = Entry(root)
name_textBox.grid(row=0, column=0)
vcmd = root.register(validate_name)
name_textBox.config(validate="focusout", validatecommand=(vcmd,'%P'))
    
    
lname_textBox = Entry(root)
lname_textBox.grid(row=1, column=0)
vcmd1 = root.register(validate_lname)
lname_textBox.config(validate="focusout",validatecommand=(vcmd1,'%P'))
    
root.mainloop()

是Python和Tkinter的新手,我想在一个应用程序上触发验证,其中形式的条目彼此相邻。 输入输入后,如果我将光标转移到另一个输入,我的应用程序会冻结。 我该如何解决 我希望每个条目都验证并使应用程序停止冻结。 我没有从中得到任何错误,只是冻结了。 我不知道为什么会发生。 我已经搜索和没有看到这里的任何答案

import tkinter
from tkinter import *
   
def validate_name(name):
    if len(name) > 2 and not name.isdigit():
        name_textBox.config(bg='#C4C4C4')      
        return True
    elif any(ch.isdigit() for ch in name):
        name_textBox.config(bg='#F56B6F')                
        name_textBox.delete(0, END)  
        name_textBox.focus_set() 
        return False  
    elif len(name) == 0:                    
        name_textBox.config(bg='#F56B6F')
        name_textBox.delete(0, END)  
        name_textBox.focus_set() 
        return False
    elif len(name) <= 2:
        name_textBox.config(bg='#F56B6F')
        name_textBox.delete(0, END)  
        name_textBox.focus_set() 
        return False
    else:                  
        name_textBox.config(bg='#F56B6F') 
        name_textBox.delete(0, END)  
        name_textBox.focus_set() 
        return False                  
                      
def validate_lname(lname):     
    if len(lname) > 2 and not lname.isdigit():
        lname_textBox.config(bg='#FFFFFF')      
        return True
    elif any(ch.isdigit() for ch in lname):
        lname_textBox.config(bg='#F56B6F')                
        lname_textBox.delete(0, END)  
        lname_textBox.focus_set() 
        return False  
    elif len(lname) == 0:                    
        lname_textBox.config(bg='#F56B6F')
        lname_textBox.delete(0, END)  
        lname_textBox.focus_set() 
        return False
    elif len(lname) <= 2:
        lname_textBox.config(bg='#F56B6F')
        lname_textBox.delete(0, END)  
        lname_textBox.focus_set() 
        return False
    else:                  
        lname_textBox.config(bg='#F56B6F')                
        lname_textBox.delete(0, END)  
        lname_textBox.focus_set() 
        return False   

                      

root = Tk()  
name_textBox = Entry(root)
name_textBox.grid(row=0, column=0)
vcmd = root.register(validate_name)
name_textBox.config(validate="focusout", validatecommand=(vcmd,'%P'))
    
    
lname_textBox = Entry(root)
lname_textBox.grid(row=1, column=0)
vcmd1 = root.register(validate_lname)
lname_textBox.config(validate="focusout",validatecommand=(vcmd1,'%P'))
    
root.mainloop()

Am new to Python and Tkinter i want to trigger validation on an of a app, where entries in a form are adjacent to each other.
After input into entry if i move my cursor to another entry my app freezes.
How do i fix it
I want each entry to validate and for the App to stop freezing.
I didnt get any error out of it, it simply just froze.
I have no idea why it is happeing.
I have have searched and havent seen any answer about here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2025-01-25 12:42:57

如果我将光标转移到另一个条目,则输入进入条目后
冻结。我该如何修复它,我希望每个条目都验证

两个功能都是相同的,因为重复了。实际上。您不需要它。
tkinter导入 *避免通配符
您应该使用导入tkinter作为tk而不是从tkinter导入 *

添加两个函数:

  • testVal()
  • go_to_to_to_next_entry()
  • 然后做looping 儿童的孩子...
  • 然后添加entry.bind('&lt; return&gt;',...

sippet:

import tkinter as tk

                       
root = tk.Tk()

def testVal(inStr, acttyp):
    if acttyp == '1':
        if not inStr.isdigit():
            return False
    return True

name_textBox = tk.Entry(root, validate="key")
name_textBox['validatecommand'] = (name_textBox.register(testVal),'%P','%d')
name_textBox.grid(row=0, column=0)
#vcmd = root.register(validate_name)
#name_textBox.config(validate="focusout", validatecommand=(vcmd,'%P'))
    
    
lname_textBox = tk.Entry(root, validate="key")
lname_textBox['validatecommand'] = (lname_textBox.register(testVal),'%P','%d')
lname_textBox.grid(row=1, column=0)
#vcmd1 = root.register(validate_lname)
#lname_textBox.config(validate="focusout",validatecommand=(vcmd1,'%P'))

def go_to_next_entry(event, entry_list, this_index):
    next_index = (this_index + 1) % len(entry_list)
    print(entry_list[this_index].get())
    entry_list[next_index].focus_set()

  
entries = [child for child in root.winfo_children() if isinstance(child, tk.Entry)]
for idx, entry in enumerate(entries):
    entry.bind('<Return>', lambda e, idx=idx: go_to_next_entry(e, entries, idx))

    
root.mainloop()

After input into entry if i move my cursor to another entry my app
freezes. How do i fix it I want each entry to validate

Both functions are the same, because of duplicated. Actually. you don't need it.
Avoid wildcard from tkinter import *
You should use import tkinter as tk instead of from tkinter import *

Add two function:

  • testVal()
  • go_to_next_entry()
  • Then do looping child for child...
  • Then add entry.bind('<Return>',...

Snippet:

import tkinter as tk

                       
root = tk.Tk()

def testVal(inStr, acttyp):
    if acttyp == '1':
        if not inStr.isdigit():
            return False
    return True

name_textBox = tk.Entry(root, validate="key")
name_textBox['validatecommand'] = (name_textBox.register(testVal),'%P','%d')
name_textBox.grid(row=0, column=0)
#vcmd = root.register(validate_name)
#name_textBox.config(validate="focusout", validatecommand=(vcmd,'%P'))
    
    
lname_textBox = tk.Entry(root, validate="key")
lname_textBox['validatecommand'] = (lname_textBox.register(testVal),'%P','%d')
lname_textBox.grid(row=1, column=0)
#vcmd1 = root.register(validate_lname)
#lname_textBox.config(validate="focusout",validatecommand=(vcmd1,'%P'))

def go_to_next_entry(event, entry_list, this_index):
    next_index = (this_index + 1) % len(entry_list)
    print(entry_list[this_index].get())
    entry_list[next_index].focus_set()

  
entries = [child for child in root.winfo_children() if isinstance(child, tk.Entry)]
for idx, entry in enumerate(entries):
    entry.bind('<Return>', lambda e, idx=idx: go_to_next_entry(e, entries, idx))

    
root.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文