我的问题是我在 tkinter 中制作了一个 GUI,它绘制了一些地图和信息,并且该部分运行良好,但是当我尝试使用窗口的 X 按钮关闭程序时,该进程仍在运行,我必须终止带有停止按钮的进程,如在这个问题中所示,他们建议在CMD中运行,我在Windows上执行此操作并检查任务管理器,关闭程序后该进程仍在继续,我能做些什么来解决这个问题? 这是脚本的结构:
#This is some figure where i plot the maps
#marco de imagen principal
f = plt.figure(figsize=(3,3))
axe = f.add_subplot(111)
#axe = f.add_subplot(111)
#plt.axis('off')
axe.format_coord = lambda x, y: ''
#marco de ploteo de mapa
figu=plt.Figure(figsize=(2,2))
#ploteo=figu.add_subplot(111)
ploteo=figu.add_axes([0, 0, 1, 1])
ploteo.format_coord = lambda x, y: ''
plt.axis('off')
Class Inicio(tk.Tk):
def __init__(self, *args, **kwargs):
#I initialize all the menu, labels, variables and canvas and some other functions
def libro(self):
def onpress(self,event):
.
.
.
and more funtions
ex = Inicio()
ex.geometry("1280x720")
ex.mainloop()
也许问题与剧情有关?
您是否尝试过添加一个协议来处理
X
按钮的关闭?
import tkMessageBox as tmb
def __init__(self, *args, **kwargs):
# All your stuff
self.protocol('WM_DELETE_WINDOW', self.close_app)
def close_app(self):
if tmb.askokcancel("Close", "Are you sure...?"):
self.destroy()
这假设
self.destroy()
通过 root
应用于您的 ex.mainloop()
TKinter 框架,因为它是您最外层的调用。由 self.destroy()
创建的框架上的 ex.mainloop()
也将结束 mainloop()
的 root
。
我在使用 Tkinter 和 matplotlib 时遇到了同样的问题,对我有用的是添加
fig = plt.figure()
plt.close() # add this line of code
ax = fig.add_subplot(111, projection='3d')
我想当在后台创建 plt.figure() 时,会创建一个你看不到的 tk 窗口,因此 python 脚本永远不会结束。