我用 Tkinter 制作了一個有限制的程式,并創建了一個自定義錯誤訊息,其中如果滿足限制條件,則會彈出一個錯誤訊息框。
現在,我的問題是訊息框僅在終止我的程式后才會彈出,在程式仍然打開時不會執行,這是我的代碼。
try:
None
except:
raise SyntaxError(messagebox.showerror('Error', 'Error message')
這是輸出:
Tkinter 回呼 Traceback 中的例外(最近一次呼叫最后一次):
檔案“C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\tkinter_init _.py ”,第 1921 行,呼叫 回傳 self.func( *args) 檔案“C:\Users\user1\PycharmProjects\Python Program\main.py”,第 53 行,在 command=lambda: equal(),relief=FLAT,borderwidth=1) 檔案“C:\Users\user1 \PycharmProjects\Python Porgram\main.py",第 32 行,結果相等 = str(eval(expression)) 檔案 "",第 1 行 */ ^ SyntaxError: invalid syntax Traceback (last 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
行程以退出代碼 1 結束
uj5u.com熱心網友回復:
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')
如果您不知道在您的 中可以引發哪些型別的例外try,您可以將 替換except SyntaxError:為except:。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/455121.html
