简体   繁体   English

tkinter - 如果错误未修复,如何不处理进一步的代码 - messagebox.showerror()

[英]tkinter - How to not process further code if error is not fixed - messagebox.showerror()

I have a tkinter app where I have Labels, Entries and Button.我有一个 tkinter 应用程序,其中有标签、条目和按钮。 Whenever I click the button, the entries get passed to a function where they are verified according to a format.每当我单击该按钮时,条目都会传递到 function 并根据格式对其进行验证。

For example -例如 -

Two fields - Employee Name, Employee Id两个字段——员工姓名、员工编号

Now, I wish to check if username starts with "EMP" or not.现在,我想检查用户名是否以“EMP”开头。 For that I have made some functions(like checks if alphanumeric or not etc).为此,我做了一些功能(比如检查字母数字等)。 If the username does not start with "EMP", what I am doing now is showing an error box like this如果用户名不是以“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.  <-------

Now, after the user clicks "Ok" to any prompt, The code which I don't want user to access still accesses.现在,在用户对任何提示单击“确定”后,我不希望用户访问的代码仍然可以访问。

How to not user process further until he fixes the errors from the above checks?在他修复上述检查中的错误之前,如何不让用户进一步处理?

You could approach this in a do-while manner (even if pyhton does not support this semantically)你可以以一种do-while方式来处理这个问题(即使 pyhton 在语义上不支持这个)

Pseudo code would look like this:伪代码如下所示:

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

EDIT: I forgot to note that, as mentioned below, this would have to be done in an extra thread.编辑:我忘了注意,如下所述,这必须在一个额外的线程中完成。

Another thing that might work is putting the dialogs in a method that calls itself if the name is invalid to start over.另一件可能有效的事情是将对话框放在一个方法中,如果名称无效重新开始,该方法会调用自身。

But I never tried that and cannot test it since I am commuting right now.但我从未尝试过,也无法测试,因为我现在正在上下班。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM