为什么 TkInter 在出现未处理的异常后不会停止运行?
看下面的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论