简体   繁体   English

将matplotlib嵌入到tkinter canvas中可打开两个窗口

[英]Embedding matplotlib into tkinter canvas opens two windows

The following code I am working on in not behaving the way I wish it to. 下面的代码正在以我不希望的方式工作。 I have embedded a matplotlib graph into a tkinter canvas. 我已经将matplotlib图形嵌入到tkinter画布中。 The program opens up two windows, one of which functions properly, and one of which is not necessary.I am not sure how to fix this. 该程序将打开两个窗口,其中一个窗口可以正常运行,而其中一个窗口不是必需的。我不确定如何解决此问题。 Here is the code, please ignore the unnecessary imports :) 这是代码,请忽略不必要的导入:)

import numpy as np
import sys
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib as mpl
from matplotlib import cm
from numpy.random import random
from matplotlib.widgets import Button
import matplotlib.colors
import tkinter as tk
import matplotlib.backends.tkagg as tkagg
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

DEBUG_MODE = False                          #Debug mode - True = ON
MATRIX_WIDTH = 50
MATRIX_HEIGHT = 50
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
LED_COUNT = MATRIX_WIDTH * MATRIX_HEIGHT
REFRESH_RATE = 30                           #REFRESH_RATE used to control FuncAnimation interval
MATRIX = random((50,50))                    #Fills MATRIX as not to be null for first print  


plt.rcParams['toolbar'] = 'None'            #Disables matplotlib toolbar
fig = plt.figure(figsize=(3,3))             #'figsize' measured in inches
im = plt.imshow(MATRIX, interpolation='nearest', cmap=cm.Spectral)
plt.axis('off')                             #Turns off x, y axis

def data_gen():                             #Generates amd populates MATRIX with pattern data
    while True:        
        MATRIX = random((MATRIX_WIDTH, MATRIX_HEIGHT))
        yield MATRIX
        if (DEBUG_MODE): print("MATRIX yeilded")

def update(data):                           #Updates/prints new MATRIX from data_gen()
    im.set_array(data)
    if (DEBUG_MODE): print("Updated data")



root = tk.Tk()
label = tk.Label(root,text="Matrix Program").grid(column=0, row=0)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(column=0,row=1)


ani = animation.FuncAnimation(fig, update, data_gen, interval=REFRESH_RATE)
plt.show()

What needs to be done to this code so that it opens only one canvas from tkinter with the live matplotlib graph embedded? 为了使该代码仅打开tkinter中嵌入了实时matplotlib图形的一个画布,需要对这段代码进行什么处理?

How can I set the size of the canvas? 如何设置画布的尺寸?

Do not call plt.show() if you want to show your figure inside a tk GUI. 如果要在tk GUI中显示图形,请不要调用plt.show() Best do not use pyplot at all when embedding. 嵌入时最好不要使用pyplot

On the other hand, you probably want to start the mainloop, tk.mainloop() , at some point. 另一方面,您可能想在某个时候启动mainloop tk.mainloop()

Refer to the matplotlib example on how to embedd a matplotlib figure into tk. 请参阅matplotlib示例,以了解如何将matplotlib图形嵌入到tk中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM