-1

I want to put matplotlib into a main tkinter window but after following numerous tutorials, I am still struggling to put matplotlib in the main window.

The reason why i need it is because im trying to make a graphing software for a school project.

any help will be much appreciated, here is my code so far..

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
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Priyen Patel
  • 11
  • 1
  • 2
  • 1
    Welcome to Stack Overflow! What is the error that you are experiencing at the moment? And what have you tried so far? This extra information will help other users help you. – meenaparam Aug 25 '17 at 16:33
  • Maybe I didnt understand you problem, You want to inport matplotlib into the same file you are importing tkinter? – Breno Baiardi Aug 25 '17 at 16:35
  • @BrenoBaiardi yes, I'm trying to make a graphing simulator for students to use hence why I'm trying to bring in the matplotlib module into a tkinter window – Priyen Patel Aug 29 '17 at 13:44

2 Answers2

2

Essentially, you are attempting to embed a matplotlib graph inside a Tkinter window. The key thing to note is that you will need to use the TkAgg backend for matplotlib so it is graphable in Tkinter. Here is a small example showing you how to embed the plot inside a Tkinter window and add a button:

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()

Chose sine/cosine waves to show that triggers on Tkinter objects, e.g. buttons, can manipulate matplotlib plots.

These links are very helpful when trying to embed matplotlib in Tkinter:

Advait
  • 181
  • 6
1

I believe you will find something similar to what you are trying to do in the following links. After some search I found them to be very usefull when trying to insert graphics in tkinter and treating them as frames

Example one

Example two

Hope it helps

Breno Baiardi
  • 99
  • 1
  • 16