4

I am using tkinter to get display a histogram plotted by matplotlib. For some reason though i am getting FigureCanvasTkAgg object has no attribute show but for many it seems to be working. Besides this i even tried using .draw() and i get the error "'NoneType' object has no attribute 'get_tk_widget' " this is the data

{   "ts": 1393631990,    "visitor_uuid": "9a83c97f415601a6",    "visitor_username": null,    "visitor_source": "external",    "visitor_device": "browser"}
{   "ts": 1393631989,    "visitor_uuid": "745409913574d4c6",    "visitor_username": null,    "visitor_source": "external",    "visitor_device": "browser"}
{   "ts": 1393631989,    "visitor_uuid": "64bf70296da2f9fd",    "visitor_username": null,    "visitor_source": "internal",    "visitor_device": "browser"}
from tkinter import * 
import os
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg


def openWindowForT2():
    with open('Data.json', 'r') as file1:
        df= pd.read_json(file1, lines=True)



    windowforT2 = Toplevel(window)
 
    # sets the title of the
    # Toplevel widget
    windowforT2.title("New Window")
 
    # sets the geometry of toplevel
    windowforT2.geometry("600x600")
 


    frame = Frame(windowforT2)
    frame.place(relx=0.5, rely=0.1,relwidth=0.75 ,relheight=0.1,anchor='n')

    nam = Entry(frame,font=40)
    nam.place(relwidth=0.65, relheight=1)
    button1 = Button(frame, text= "Get graph")
    button1.place(relx=0.7,relwidth=0.3,relheight=1)

    x1 = df["name"]
    plt.hist(x1, density=True, bins=30)
    plt.ylabel("time")
    plt.xlabel("val")
    
    f = Figure(figsize=(25,15))
    canvas = FigureCanvasTkAgg(f,master = windowforT2).show()
  
    canvas.get_tk_widget().pack(side= TOP, fill=BOTH,expand=True)

if i just do plt.show() im able to see that histogram is being produced but i want it to be inside the tkinter window. can someone tell me why this is causing an error?

newbieperson
  • 79
  • 2
  • 7
  • 1
    Does this answer your question? [Why does my pie chart in Tkinter does not show?](https://stackoverflow.com/questions/52604063/why-does-my-pie-chart-in-tkinter-does-not-show) – Reti43 Dec 05 '21 at 19:42
  • I saw this before and it does not. I tried doing .draw() and get nonType error – newbieperson Dec 05 '21 at 19:52
  • I even tried doing canvas.show() but im getting the error "AttributeError: 'FigureCanvasTkAgg' object has no attribute 'show' " I meant i even tried doing a canvas.draw() but with that i get NoneType. – newbieperson Dec 05 '21 at 20:05
  • 2
    You're dealing with two issues. `FigureCanvasTkAgg` has deprecated the method `show` and now requires `draw`. When you use `show`, you get an error for that. When you fix that, you get an error in the next line because above you did `canvas = FigureCanvasTkAgg().draw()`. This saves the result of `draw` to `canvas` which is None. This is similar in spirit to [this question](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name). Don't chain your methods like that. Break up the creation of the canvas object from drawing it. – Reti43 Dec 05 '21 at 20:06
  • 2
    Ah yes. i got it. Now its working ^^ thank you – newbieperson Dec 05 '21 at 20:12

1 Answers1

1

You can use the draw() instead of show() attribute.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 25 '22 at 06:21