为什么 TkInter 在出现未处理的异常后不会停止运行?

发布于 2025-01-15 00:12:35 字数 338 浏览 4 评论 0原文

看下面的代码:

from tkinter import *

def Raise():
    raise


Window = Tk()
Window.geometry("200x100")

# Raise()

Raise1 = Button(Window,text="Raise", command=Raise)
Raise1.pack()

Window.mainloop()

跟着我在这里:

直接调用 Raise():

如果 Raise() 中的 #comment 被删除,它会调用 Raise() 函数,这反过来会引发一个未处理的异常,按照我的预期停止程序。

Take the bellow code:

from tkinter import *

def Raise():
    raise


Window = Tk()
Window.geometry("200x100")

# Raise()

Raise1 = Button(Window,text="Raise", command=Raise)
Raise1.pack()

Window.mainloop()

Follow me here:

Calling Raise() directly:

If the #comment from Raise() is removed, it calls the Raise() function, which in turn, raises an Unhandled Exception that stops the program as I expected. ????

The message that appears is something like this:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-71427e5db6ac> in <module>
      8 Window.geometry("200x100")
      9 
---> 10 Raise()
     11 
     12 Raise1 = Button(Window,text="Raise", command=Raise)

<ipython-input-1-71427e5db6ac> in Raise()
      2 
      3 def Raise():
----> 4     raise
      5 
      6 

RuntimeError: No active exception to reraise

Calling Raise() through a tkinter button:

But if Raise1 button is clicked, although the same function is called something different happens.
This time Python shows a error message in the console, but Tkinter keeps running. Not what I was expecting. ????

The message that appears now is different, is something like this:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "<ipython-input-1-1fb7f05e6ef7>", line 4, in Raise
    raise
RuntimeError: No active exception to reraise

Since the button triggers the same function as the direct call, shouldn't the behavior to be the same?

Can somebody tell why this happens, and how we are supposed to handle this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文