2017-08-25 33 views
-2

我想将matplotlib放入主tkinter窗口中,但在遵循大量教程之后,我仍然努力将matplotlib放在主窗口中。在tkinter窗口中合并matplotlib

我需要它的原因是因为我试图为学校项目制作图形软件。

任何帮助将非常感激,这是我到目前为止的代码..

from tkinter import * #importing all files from Tkinter so all libraries are avaiable 

root = Tk()   #root is a variable which equals a tkinter class - creates blank window 

root.geometry('1600x900') # Size 1600, 900 for the window 

#----------------------------------------------------------------------- 
--------------- 

button1xy = Button(text="xy") 
buttony = Button(text="y=") 
buttonclrscrn = Button(text="clear screen") 
buttonbestfit = Button(text="line of best fit")#labels for the buttons 



button1xy.grid(row=0) 
buttony.grid(row=0, column=1) 
buttonclrscrn.grid(row=0, column=2) 
buttonbestfit.grid(row=0, column=3) #displaying them on the screen, 
grid method 


menu = Menu(root) 
root.config(menu=menu) 

def dropdownmenu(): 
     submenu = Menu(menu) 
     menu.add_cascade(label=">", menu=submenu) 
     submenu.add_command(label="Linear") 
     submenu.add_command(label="Polynomial") 
     submenu.add_command(label="Trigonometrical") 
     submenu.add_command(label="Percentage Error") #drop down menu 
     which appears in toolbar when button is clicked on 


    buttonmenu = Button(text=">", command=dropdownmenu) #label and 
    command for the drop down menu button 

    buttonmenu.grid(row=0, column=8) #grid method for it to display 


    #------------------------------------------------------------------- 


    root.mainloop() #continuously keeps window on the screen until closed 
      #generates window 
+1

欢迎堆栈溢出时是非常有用的!您目前遇到的错误是什么?你到目前为止尝试过什么?这些额外的信息将帮助其他用户帮助您。 – meenaparam

+0

也许我不明白你的问题,你想将matplotlib导入你正在导入tkinter的同一个文件? –

+0

@BrenoBaiardi是的,我正在尝试制作一个图形模拟器供学生使用,因此我试图将matplotlib模块放入tkinter窗口 –

回答

2

本质上讲,你正在试图嵌入一个Tkinter的窗口内matplotlib图。关键要注意的是,您需要将TkAgg后端用于matplotlib,因此它在Tkinter中可以被绘制。这里是表示你如何嵌入一个Tkinter的窗口内的情节,并添加一个按钮一个小例子:

import tkinter as tk 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 
import matplotlib.pyplot as plt 
import matplotlib 
import math 

def updateGraph(): 
    """Example function triggered by Tkinter GUI to change matplotlib graphs.""" 
    global currentGraph 
    # Clear all graphs drawn in figure 
    plt.clf() 
    y = [] 
    if currentGraph == "sin": 
     for i in x: 
      y.append(math.cos(i)) 
     currentGraph = "cos" 
    else: 
     for i in x: 
      y.append(math.sin(i)) 
     currentGraph = "sin" 
    plt.plot(x,y) 
    fig.canvas.draw() 

# This defines the Python GUI backend to use for matplotlib 
matplotlib.use('TkAgg') 

# Initialize an instance of Tk 
root = tk.Tk() 

# Initialize matplotlib figure for graphing purposes 
fig = plt.figure(1) 

# Special type of "canvas" to allow for matplotlib graphing 
canvas = FigureCanvasTkAgg(fig, master=root) 
plot_widget = canvas.get_tk_widget() 

# Example data (note: default calculations for angles are in radians) 
x = [] 
for i in range(0, 500): 
    x.append(i/10) 
y = [] 
for i in x: 
    y.append(math.sin(i)) 
plt.plot(x, y) 

currentGraph = "sin" 

# Add the plot to the tkinter widget 
plot_widget.grid(row=0, column=0) 
# Create a tkinter button at the bottom of the window and link it with the updateGraph function 
tk.Button(root,text="Update",command=updateGraph).grid(row=1, column=0) 

root.mainloop() 

之所以选择的正弦/余弦波以表明触发上Tkinter的对象,例如按钮,可以操作matplotlib图。

这些链接试图Tkinter的嵌入matplotlib时是非常有帮助的:

0

我相信你会发现类似的东西你正在尝试在以下链接中做。一些搜索后,我发现他们试图插入Tkinter的图形,并把它们当作帧

Example one

Example two

希望它可以帮助