Tkinter GUI中Matplotlib图之间的干扰

时间:2018-06-13 22:05:32

标签: matplotlib tkinter tkinter-canvas

我学会了如何通过这篇帖子https://matplotlib.org/examples/user_interfaces/embedding_in_tk.html将matploblib图嵌入到Tkinter GUI中。看起来工作正常。

但是,我遇到了让多个matplotlib图同时正常工作的问题,让我在这里解释一下。下面的代码生成两个按钮,每个按钮链接到一个带有两个新按钮(加载数据和绘图数据)的新窗口。您可以生成一些虚拟的两列数据,比如data1.txt,data2.txt,data3.txt,data4.txt,用于绘图。我观察到的是(1)如果我单独调用窗口,我可以加载和绘制data1.txt,data2.txt,data3.txt,data4.txt。但是,如果我同时打开两个窗口,我只能在第二个窗口中自由正确地绘图,而第一个窗口则不显示任何内容。似乎第一个窗口被第二个窗口的存在所抑制。

任何人都可以帮助我了解这里发生的事情。我的matplotlib版本是2.0.2。 Tkinter版本是$ Revision:81008 $。 Python版本2.7.15。谢谢!

from Tkinter import *
import Tkinter as tk
import ttk
import tkFileDialog
import numpy
##loading matplotlib modules
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.gridspec as gridspec


class Overall_Look:
    def __init__(self, master):
        self.master = master

        self.top_frame = ttk.Frame(self.master, padding = (10, 10))
        self.top_frame.pack()

        ##here are the layout for step 1, load structure files

        ttk.Button(self.top_frame, text = "Button_1", command = self.plot_one,
                   style = "TButton").grid(row = 1, column = 0, columnspan = 2, padx = 5, sticky = "sw")
        ttk.Button(self.top_frame, text = "Button_2",command = self.plot_two,
                   style = "TButton").grid(row = 1, column = 2, columnspan = 2, padx = 5, sticky = "sw")

    def plot_one(self):
        self.plot_one = tk.Toplevel(self.master)
        self.GUI = Plot_One(self.plot_one)

    def plot_two(self):
        self.plot_two = tk.Toplevel(self.master)
        self.GUI = Plot_Two(self.plot_two)


class Plot_One():
    def __init__(self, master):
        self.master = master

        self.top_frame = ttk.Frame(self.master, padding = (10, 10))
        self.top_frame.pack()

        ##here are the layout for step 1, load structure files

        ttk.Button(self.top_frame, text = "Load Data 1", command = self.load_data_1,
                             style = "TButton").grid(row = 1, column = 0, columnspan = 2, padx = 5, sticky = "sw")
        ttk.Button(self.top_frame, text = "Plot Data 1",command = self.start_plot_one,
                             style = "TButton").grid(row = 1, column = 2, columnspan = 2, padx = 5)

        self.bottom_frame = ttk.Frame(self.master, padding = (10, 10))
        self.bottom_frame.pack()

        self.fig_1 = plt.figure(figsize=(5, 5), dpi=100) ##create a figure; modify the size here
        self.fig_1.add_subplot(111)

        self.fig_1.tight_layout()

        self.canvas = FigureCanvasTkAgg(self.fig_1, master = self.bottom_frame)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
        self.toolbar = NavigationToolbar2TkAgg(self.canvas, self.bottom_frame)
        self.toolbar.update()
        self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)


    def load_data_1(self):
        self.data_1 = tkFileDialog.askopenfilename(defaultextension = ".txt",
                                                             filetypes = [("Text Documents", "*.txt")])

    def start_plot_one(self):

        data = numpy.loadtxt(self.data_1).transpose()
        x = data[0]
        y = data[1]

        self.fig_1.clf()
        self.fig_1.add_subplot(111)
        plt.plot(x, y, 'b-', lw=2)
        self.fig_1.tight_layout()
        self.canvas.draw()

class Plot_Two():
    def __init__(self, master):
        self.master = master

        self.top_frame = ttk.Frame(self.master, padding = (10, 10))
        self.top_frame.pack()

        ##here are the layout for step 1, load structure files

        ttk.Button(self.top_frame, text = "Load Data 2", command = self.load_data_2,
                   style = "TButton").grid(row = 1, column = 0, columnspan = 2, padx = 5, sticky = "sw")
        ttk.Button(self.top_frame, text = "Plot Data 2",command = self.start_plot_two,
                  style = "TButton").grid(row = 1, column = 2, columnspan = 2, padx = 5)

        self.bottom_frame = ttk.Frame(self.master, padding = (10, 10))
        self.bottom_frame.pack()

        self.fig_2 = plt.figure(figsize=(5, 5), dpi=100) ##create a figure; modify the size here
        self.fig_2.add_subplot(111)

        self.fig_2.tight_layout()

        self.canvas = FigureCanvasTkAgg(self.fig_2, master = self.bottom_frame)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
        self.toolbar = NavigationToolbar2TkAgg(self.canvas, self.bottom_frame)
        self.toolbar.update()
        self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)


    def load_data_2(self):
        self.data_2 = tkFileDialog.askopenfilename(defaultextension = ".txt",
        filetypes = [("Text Documents", "*.txt")])

    def start_plot_two(self):

        data = numpy.loadtxt(self.data_2).transpose()
        x = data[0]
        y = data[1]

        self.fig_2.clf()
        self.fig_2.add_subplot(111)
        plt.plot(x, y, 'b-', lw=2)
        self.fig_2.tight_layout()
        self.canvas.draw()

def main():
    root = Tk()
    GUI = Overall_Look(root)
    root.mainloop()

if __name__ == "__main__": main()

0 个答案:

没有答案