Python 在matplotlib窗口打开时关闭tkinter progressbar窗口

Python 在matplotlib窗口打开时关闭tkinter progressbar窗口,python,matplotlib,plot,tkinter,Python,Matplotlib,Plot,Tkinter,我正在写一个程序,它做一些工作,并使用matplotlib来绘制一些数据。这可能需要一些时间,所以我使用tkinter设置了一个progressbar。用tkinter穿线并不是那么容易。我在主线程中运行progressbar,在子线程中运行我的工作内容。但是,我不能在工作完成后关闭progressbar窗口,因为matplotlib显然在tk根窗口中做了一些事情。我不知道是什么。我添加了一个我正在尝试做的最小示例。请注意,删除行“plotsomething()”可以实现我想要的功能:完成工作后

我正在写一个程序,它做一些工作,并使用matplotlib来绘制一些数据。这可能需要一些时间,所以我使用tkinter设置了一个progressbar。用tkinter穿线并不是那么容易。我在主线程中运行progressbar,在子线程中运行我的工作内容。但是,我不能在工作完成后关闭progressbar窗口,因为matplotlib显然在tk根窗口中做了一些事情。我不知道是什么。我添加了一个我正在尝试做的最小示例。请注意,删除行“plotsomething()”可以实现我想要的功能:完成工作后关闭进度条

您能帮我弄清楚如何在不关闭matplotlib窗口的情况下关闭progressbar窗口吗

# coding = utf-8
import numpy as np
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import ttk
import threading, queue
import time

def MAIN():
    PB = q.get()
    for i in np.arange(10):
        time.sleep(0.2)
        print(i)
        PB.step(10)
        PB.update()
    print("Done")

def plotsomething():
    x = np.linspace(0,10,100)
    y = np.sin(x)
    plt.plot(x,y)

root = tk.Tk()
root.title("Progress")
PB = ttk.Progressbar(root, orient = "horizontal",length=300, mode = 'determinate')
PB.pack()
q = queue.Queue()
q.put(PB)

plotsomething()

T = threading.Thread(target=MAIN(), name="MAIN")
T.start()
T.join()
plt.show()
编辑-解决方案:我现在通过使用matplotlib tk后端分别绘制每个窗口来解决问题。显然PyPlot干扰了tkinter根窗口。有关更多详细信息和提示,请参阅tcaswell的评论。多谢各位

import numpy as np
import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import tkinter as tk
from tkinter import ttk
import queue, threading, time

def center_window(window_parent, w=300, h=20):
    # get screen width and height
    ws = window_parent.winfo_screenwidth()
    hs = window_parent.winfo_screenheight()
    # calculate position x, y
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    window_parent.geometry('%dx%d+%d+%d' % (w, h, x, y))

def MAIN():
    PB = q.get()
    for i in np.arange(10):
        time.sleep(0.2)
        print(i)
        PB.step(10)
        PB.update()
    print("Done")

root = tk.Tk()
root.wm_title("Embedding in TK")


f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)

a.plot(t,s)
a.set_title('Tk embedding')
a.set_xlabel('X axis label')
a.set_ylabel('Y label')


#a tk.DrawingArea
root2 = tk.Tk()

PB = ttk.Progressbar(root2, orient = "horizontal",length=300, mode = 'determinate')
PB.pack()

canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

toolbar = NavigationToolbar2TkAgg( canvas, root )
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

root2.iconify()
root2.update()
root2.deiconify()

center_window(root2)

q = queue.Queue()
q.put(PB)

T = threading.Thread(target=MAIN(), name="MAIN")
T.start()
T.join()

root2.quit()
root2.destroy()

tk.mainloop()

您正在启动的
TK
与启动的
TK
之间的gui主循环冲突。如果要将
matplotlib
与自己的gui一起使用,则必须自行嵌入,并且无法导入
pyplot
。所有使
pyplot
界面精彩的幕后魔法都让你在这里感到困惑

有关教程,请参见,以了解嵌入如何实现mpl

另见:


因此,您希望progressbar在启动时启动,数到10,打印“完成”,销毁自身,然后matplotlib窗口将立即启动?作为一个附带问题,您希望在文档中的什么地方找到它?这个问题每隔一周就会以各种形式出现,这表明存在文档失败。再看一次这个问题,你真的应该将你的编辑作为答案发布。愤世嫉俗地说,我的半答案得到了代表,而你的完整答案应该得到代表;)