Pycharm IDE中带有停止按钮的停止脚本抛出KeyboardInterrupt错误



我用Tkinter创建了一个窗口。当我按下pycharm IDE上的停止按钮(红色方块)时,程序停止(这很好)并且我得到一条错误消息(这很糟糕):

Traceback (most recent call last):
File "C:/Users/toman/klusterbox/playground/a.py", line 52, in <module>
win.finish()  # update and mainloop the window
File "C:/Users/toman/klusterbox/playground/a.py", line 37, in finish
mainloop()
File "C:Program FilesPython37libtkinter__init__.py", line 560, in mainloop
_default_root.tk.mainloop(n)
KeyboardInterrupt

我正在运行以下准备运行的python脚本:

from tkinter import *

class MakeWindow:  # builds a window object
def __init__(self):
self.topframe = Frame(root)  # creates a frame
self.s = Scrollbar(self.topframe)  # scrollbar
self.c = Canvas(self.topframe, width=1600)  # canvas
self.body = Frame(self.c)  # creates a frame on the canvas
self.buttons = Canvas(self.topframe)  # creates a frame for buttons
def create(self, frame):
if frame is not None:
frame.destroy()  # close out the previous frame
self.topframe.pack(fill=BOTH, side=LEFT)
self.buttons.pack(fill=BOTH, side=BOTTOM)
# link up the canvas and scrollbar
self.s.pack(side=RIGHT, fill=BOTH)
self.c.pack(side=LEFT, fill=BOTH)
self.s.configure(command=self.c.yview, orient="vertical")
self.c.configure(yscrollcommand=self.s.set)
# link the mousewheel - implementation varies by platform
if sys.platform == "win32":
self.c.bind_all('<MouseWheel>', lambda event: self.c.yview_scroll
(int(mousewheel * (event.delta / 120)), "units"))
elif sys.platform == "darwin":
self.c.bind_all('<MouseWheel>', lambda event: self.c.yview_scroll
(int(mousewheel * event.delta), "units"))
elif sys.platform == "linux":
self.c.bind_all('<Button-4>', lambda event: self.c.yview('scroll', -1, 'units'))
self.c.bind_all('<Button-5>', lambda event: self.c.yview('scroll', 1, 'units'))
self.c.create_window((0, 0), window=self.body, anchor=NW)
def finish(self):  # This closes the window created by front_window()
root.update()
self.c.config(scrollregion=self.c.bbox("all"))
mainloop()

root = Tk()  # define root
position_x = 100  # initialize position and size for root window
position_y = 50
size_x = 625
size_y = 600
root.geometry("%dx%d+%d+%d" % (size_x, size_y, position_x, position_y))  # implements the size for the window
mousewheel = -1  # configure direction of mousewheel as reverse ( 1 for natural)
win = MakeWindow()  # create window object
win.create(None)  # build the window
for i in range(50):
Label(win.body, text=i).pack()  # fill the window with numbers
Button(win.buttons, text="Quit", width=20, command=lambda: root.destroy()).pack(side=LEFT)  # create a quit button
win.finish()  # update and mainloop the window

我做这个程序已经有一段时间了,我不记得直到最近才收到错误信息。IDE配置是否可能以某种方式发生了变化?请注意,当我点击"quit"时,错误信息不会出现。按钮或当我关闭窗口时使用"x";在窗户的右上角。在这种情况下我得到:

Process finished with exit code 0

这是应该发生的还是我错过了什么?

根据Sujay的建议,我使用了try/except。更改finish方法可以修复此问题。
def finish(self):  # This closes the window created by front_window()
root.update()
self.c.config(scrollregion=self.c.bbox("all"))
try:
mainloop()
except KeyboardInterrupt:
root.destroy()

引自Python文档:

当用户点击中断键(通常是Control-C或Delete)时引发。在执行过程中,定期检查中断。该异常继承自BaseException,以便不会被捕获exception的代码意外捕获,从而阻止解释器退出。

由于它是一种异常,您可以使用try...except

try:
from tkinter import *
.....
.....
Button(win.buttons, text="Quit", width=20, command=lambda: 
root.destroy()).pack(side=LEFT)  # create a quit button
win.finish()  # update and mainloop the window
except KeyboardInterrupt:
root.destroy()

最新更新