[英]How do I execute first my custom error message before the first Traceback?
I made a program with restrictions with Tkinter and I created a custom error message wherein if the conditions for the restriction is met, then an error message box would pop up.我制作了一个带有 Tkinter 限制的程序,并创建了一条自定义错误消息,其中如果满足限制条件,则会弹出一个错误消息框。
Now, my problem is that the message box only pops up after terminating my program, it doesn't get executed while the program is still open, here's my code.现在,我的问题是消息框只在我的程序终止后弹出,它不会在程序仍然打开时执行,这是我的代码。
try:
None
except:
raise SyntaxError(messagebox.showerror('Error', 'Error message')
This is the output:这是 output:
Exception in Tkinter callback Traceback (most recent call last):
Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\tkinter_ init _.py", line 1921, in call return self.func(*args) File "C:\Users\user1\PycharmProjects\Python Program\main.py", line 53, in command=lambda: equal(), relief=FLAT, borderwidth=1) File "C:\Users\user1\PycharmProjects\Python Porgram\main.py", line 32, in equal result = str(eval(expression)) File "", line 1 */ ^ SyntaxError: invalid syntax Traceback (most recent call last): File "C:\Users\user1\PycharmProjects\Python Program\main.py", line 171, in raise SyntaxError(messagebox.showerror('Error', 'You cannot bundle two or more operations together.')) SyntaxError: ok文件“C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\ tkinter_init_.py ”,第 1921 行,在调用返回 self.func(*args) 文件“C:\Users\user1\ PycharmProjects\Python Program\main.py", line 53, in command=lambda: equal(), relief=FLAT, borderwidth=1) File "C:\Users\user1\PycharmProjects\Python Porgram\main.py", 行32、in equal result = str(eval(expression)) File "", line 1 */ ^ SyntaxError: invalid syntax Traceback (most recent call last): File "C:\Users\user1\PycharmProjects\Python Program\main. py", line 171, in raise SyntaxError(messagebox.showerror('Error', 'You cannot bundle two or more operations together.')) SyntaxError: ok
Process finished with exit code 1进程结束,退出代码为 1
try:
# Anything you want to do.
# If in this block a SyntaxError happens, you'll
# catch it with the line below and do whatever you want
# instead of raising and actual exception.
except SyntaxError:
messagebox.showerror('Error', 'Error message')
If you don't know what kinds of exceptions can be raised within your try
, you can replace the except SyntaxError:
with except:
.如果您不知道在您的
try
中可以引发哪些类型的异常,您可以将except SyntaxError:
替换为except:
。 But I recommend you to be explicit here with the exceptions.但我建议您在此处明确说明,但有例外。 As exceptions arises, append the specific ones.
如出现异常,append 具体的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.