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