python - 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
编辑:我忘了注意,如下所述,这必须在一个额外的线程中完成。
另一件可能有效的事情是将对话框放在一个方法中,如果名称无效,则该方法重新开始。
但我从未尝试过,也无法测试,因为我现在正在通勤。
推荐阅读
- c++ - 将本地 OpenCV Mat 变量声明为静态以防止重新分配
- memory-leaks - Kotlin伴随对象中的内存泄漏?
- javascript - 使用 scroll-spy 更改导航栏背景类
- python - scikit-learn 中带有 FeatureUnion 的自定义转换器 mixin
- youtube - videojs-youtube,如果同时设置了开始和结束参数,则两者都不起作用
- java - 如果项目已经有一个运行时类,我如何访问 java Runtime.getRuntime()?
- haskell - `State#` 的规范
- android - 从设备在 Firestore 上创建文档
- mysql - 在一个部门的所有项目中工作的员工
- c++ - 打印结构数组数据导致 Atmega 2560 重新启动