在Python中的Tkinter主窗口上放置图 [英] Placing plot on Tkinter main window in Python

查看:3489
本文介绍了在Python中的Tkinter主窗口上放置图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序处理数据然后绘制结果。通常在Python图中显示在一个新窗口中,但我想在同一个Tkinter窗口中显示图。我搜索过并找到了这两个答案, Python Tkinter在GUI中嵌入Matplotlib ,和如何刷新matplotlib图Tkinter窗口?。他们说,我们应该先制作一个画布,然后将它放在窗口上使用网格或包。这是我第一次使用canvas,我应用了这个概念,这里是一个工作示例代码,

 从matplotlib.backends.backend_tkagg导入figureCanvasTkAgg 

__author__ ='Dania'
导入numpy为np
从Tkinter导入*
导入matplotlib.pyplot为plt
class mclass:
def __init __(self,window):
self.box = Entry(window)
self.button = Button(window,text =check,command = self。
self.box.pack()
self.button.pack()

def plot(self):
x = np.array([ 2,3,4,5,6,7,8,9,10])
v = np.array([16,16.31925,17.6394,16.003,17.2861,17.3131,19.1259,18.9694,22.0003,22.81226])
p = np.array([16.23697,17.31653,17.22094,17.68631,17.73641,18.6368,
19.32125,19.31756,21.20247,22.41444,22.11718,22.12453])

plt.scatter (v,x,color ='red')
plt.plot(p,range(2 + max(x)),color ='blue')
plt.gca()。invert_yaxis

plt.suptitle(Estimation Grid,fontsize = 16)
plt.ylabel(Y,fontsize = 14)
plt.xlabel(X,fontsize = b)
plt.show()
plt.gcf()。canvas.draw()
fig = plt.figure()
canvas = FigCanvasTkAgg(fig,master =窗口)
canvas.get_tk_widget()。grid(row = 1,column = 24)
canvas.draw()

window = Tk()
start = mclass(window)
window.mainloop()

一个单独的窗口,然后显示一个灰色的画布,在网格中指定的位置没有绘图。此外,画布垂直扩展,窗口的一部分消失,换句话说,画布按下它的内容,它们不再出现,因为没有垂直滚动条出现。



我想在同一个Tkinter窗口中显示绘图,如果有任何内容被按下,我想要能够滚动窗口。



编辑:问题中添加了一个工作示例代码。



任何人都可以告诉我我做了什么错误,以及如何解决这个问题?



任何帮助。



谢谢。

解决方案




  1. 使用matplolib.figure而不是matplotlib.pyplot(并重命名所有相关函数)

  2. 添加self.window


  3. $ b p>结果:

      __ author__ ='Dania'
    import matplotlib
    matplotlib.use('TkAgg' )
    import numpy as np
    从matplotlib.backends.backend_tkagg导入图CanvasTkAgg
    从matplotlib.figure导入图
    从Tkinter导入*

    类mclass:
    def __init __(self,window):
    self.window = window
    self.box = Entry(window)
    self.button = Button(window,text =check ,command = self.plot)
    self.box.pack()
    self.button.pack()

    def plot(self):
    x = np。 array([1,2,3,4,5,6,7,8,9,10])
    v = np.array([16,16.31925,17.6394,16.003,17.2861,17.3131,19.1259,18.9694, 22.0003,22.81226])
    p = np.array([16.23697,17.31653,17.22094,17.68631,17.73641,18.6368,
    19.32125,19.31756,21.20247,22.41444,22.11718,22.12453])

    fig = figure(figsize =(6,6))
    a = fig.add_subplot(111)
    a.scatter(v,x,color ='red')
    a.plot (p,range(2 + max(x)),color ='blue')
    a.invert_yaxis()

    a.set_title(Estimation Grid,fontsize = 16)
    a.set_ylabel(Y,fontsize = 14)
    a.set_xlabel(X,fontsize = 14)

    canvas = FigCanvasTkAgg(fig,master = self.window )
    canvas.get_tk_widget()。pack()
    canvas.draw()

    window = Tk()
    start = mclass(window)
    window.mainloop()


    I have a program that processes data then plots the result. Usually in Python plots are shown in a new window, but I want to show the plot in the same Tkinter window. I've searched and found these two answers, Python Tkinter Embed Matplotlib in GUI, and How do I refresh a matplotlib plot in a Tkinter window?. They state that we should first make a canvas then place it on the window using grid or pack. This is the first time I use canvas, I've applied the concept, and here is a working sample code for that,

    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    
    __author__ = 'Dania'
    import numpy as np
    from Tkinter import *
    import matplotlib.pyplot as plt
    class mclass:
        def __init__(self,  window):
            self.box = Entry(window)
            self.button = Button (window, text="check", command=self.plot)
            self.box.pack ()
            self.button.pack()
    
        def plot (self):
            x=np.array ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
            v= np.array ([16,16.31925,17.6394,16.003,17.2861,17.3131,19.1259,18.9694,22.0003,22.81226])
            p= np.array ([16.23697,     17.31653,     17.22094,     17.68631,     17.73641 ,    18.6368,
                19.32125,     19.31756 ,    21.20247  ,   22.41444   ,  22.11718  ,   22.12453])
    
            plt.scatter(v,x,color='red')
            plt.plot(p, range(2 +max(x)),color='blue')
            plt.gca().invert_yaxis()
    
            plt.suptitle ("Estimation Grid", fontsize=16)
            plt.ylabel("Y", fontsize=14)
            plt.xlabel("X", fontsize=14)
            plt.show()
            plt.gcf().canvas.draw()
            fig = plt.figure()
            canvas = FigureCanvasTkAgg(fig, master=window)
            canvas.get_tk_widget().grid(row=1,column=24)
            canvas.draw()
    
    window= Tk()
    start= mclass (window)
    window.mainloop()
    

    The code above first shows the plot in a separated window, then shows a gray canvas without a plot in the position specified in grid. Also, the canvas expands vertically and a part of the window disappears, in other words the canvas pushes the contents bellow it down, and they don't appear anymore as no vertical scrolling bar appear.

    I want to show the plot in the same Tkinter window, and if any content is pushed down I want to be able to scroll the window.

    EDIT: A working sample code was added to the question.

    Can anyone please tell me what mistake I've done, and how to solve the problem?

    Any help is appreciated.

    Thank You.

    解决方案

    Main changes:

    1. Using matplolib.figure instead of matplotlib.pyplot (and renaming all related functions)
    2. Adding a self.window variable
    3. Using pack() everywhere (you cannot mix grid and pack in one container.)

    Result:

    __author__ = 'Dania'
    import matplotlib
    matplotlib.use('TkAgg')
    import numpy as np
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    from matplotlib.figure import Figure
    from Tkinter import *
    
    class mclass:
        def __init__(self,  window):
            self.window = window
            self.box = Entry(window)
            self.button = Button (window, text="check", command=self.plot)
            self.box.pack ()
            self.button.pack()
    
        def plot (self):
            x=np.array ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
            v= np.array ([16,16.31925,17.6394,16.003,17.2861,17.3131,19.1259,18.9694,22.0003,22.81226])
            p= np.array ([16.23697,     17.31653,     17.22094,     17.68631,     17.73641 ,    18.6368,
                19.32125,     19.31756 ,    21.20247  ,   22.41444   ,  22.11718  ,   22.12453])
    
            fig = Figure(figsize=(6,6))
            a = fig.add_subplot(111)
            a.scatter(v,x,color='red')
            a.plot(p, range(2 +max(x)),color='blue')
            a.invert_yaxis()
    
            a.set_title ("Estimation Grid", fontsize=16)
            a.set_ylabel("Y", fontsize=14)
            a.set_xlabel("X", fontsize=14)
    
            canvas = FigureCanvasTkAgg(fig, master=self.window)
            canvas.get_tk_widget().pack()
            canvas.draw()
    
    window= Tk()
    start= mclass (window)
    window.mainloop()
    

    这篇关于在Python中的Tkinter主窗口上放置图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆