tkinter-如果错误未解决,如何不处理其他代码-messagebox.showerror

问题描述

我有一个tkinter应用程序,其中有标签,条目和按钮。每当我单击按钮时,条目就会传递到一个函数,在此函数中它们会根据格式进行验证。

例如-

两个字段-员工姓名,员工ID

现在,我希望检查用户名是否以“ EMP”开头。为此,我做了一些功能(例如检查是否为字母数字等)。如果用户名不是以“ EMP”开头,那么我现在正在显示这样的错误

def tracking_images(self,emp_id,emp_name):
    emp_id,emp_name = emp_id.get(),emp_name.get()
    if len(emp_id) == 0:
        tk.messageBox.showerror("Field error","Employee ID cannot be empty")
    elif len(emp_name) == 0:
        tk.messageBox.showerror("Field error","Employee name cannot be empty")
    
    if not ValidationConditions().first_three_chars(emp_id):
        tk.messageBox.showerror("Field error","Employee ID should start with EMP")
    ........
    ........
    #Some more code which I don't want user to have until he/she fixes the error from above checks.  <-------

现在,在用户单击任何提示后的“确定”之后,仍然可以访问我不希望用户访问的代码

用户纠正上述检查中的错误之前,如何不进一步处理用户

解决方法

您可以以do-while的方式进行处理(即使pyhton在语义上也不支持)

伪代码如下:

while True:
    ask the name
    if the name passes the checks break out of the loop
    show errors

code to go to when the name is valid

编辑:我忘了注意,如下所述,这必须在额外的线程中完成。

另一种可行的方法是将对话框置于一种方法中,该方法可以在名称无效时重新调用自身。

但是由于我现在正在上下班,所以我从未尝试过并且无法测试它。