简体   繁体   English

如何在第一个 Traceback 之前首先执行我的自定义错误消息?

[英]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.

相关问题 我该如何处理此回溯名称错误? 这是我的第一个剧本 - How do i deal with this traceback name error? This is my first script 当我执行我的 python 3 程序时出现回溯错误 - Traceback error when i execute my python 3 program 即使我在调用fetchall之前执行,MySQLdb也会引发“execute()first”错误 - MySQLdb raises “execute() first” error even though I execute before calling fetchall 如何在python中引发异常之前切片回溯? - How do I slice a traceback before raising an exception in python? 如何在Python 3中解决创建SQL Lite数据库并为第一行名称和年龄打印十六进制的Traceback错误? - How to I resolve the Traceback error for creating SQL Lite database and printing a hex for first row name&age in python 3? 如何从回溯堆栈中打印第一行 - How to print the first line from a traceback stack 我的第一个代码-我是否需要添加错误消息? - My First Code - Do I need to add error messages? 如何修复我的第一个 python 代码? 名称错误,class 名称未定义 - How do I fix my first python code? Name error, class name not defined 如何将第一个操作的输出发送到第二个操作的输入? - How do I send the output of my first operation into the input of the second? 如何让我的第一个输入无效 - How do I get my first input to be invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM