-2

Below is my whole code. When I run this, I expect this to open a tkinter window and output the image in canvas when Add button is clicked. But an empty Tkinter window is triggered instead. The path is correct and when I run the same code inside the function called graph_plot separately, it works fairly good. I can't understand what's wrong.

import tkinter as tk
from tkinter.ttk import *
from pandas import DataFrame
from tkinter import *
from matplotlib import style
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import datetime

style.use('dark_background')

master=tk.Tk()
width, height = master.winfo_screenwidth(), master.winfo_screenheight()
master.geometry('%dx%d+0+0' % (width,height))
master.resizable(False,True) 
master.title('Plotter')
master.state('zoomed')
def graph_plot(type_of):
    global k
    k=tk.Toplevel()
    canvas=tk.Canvas(k,height=400,width=500)
    k.title(title.get())
    photo = tk.PhotoImage(file = 'C://Users//User//Desktop//Graph//plot1.png')
    canvas.create_image(0, 0, image=photo)   
    canvas.pack()


def save(path):
    M = tk.Toplevel()
    try:
        df1.savefig(path)
        tk.Message(M, text='Download Succesfully Completed').pack(fill=BOTH)
    except:
        tk.Message(M, text='Download Unsuccesful ').pack(fill=BOTH)
def reload():
    global title, path
    navbar = tk.Canvas(master,width=width+1,highlightthickness=0, height=40, bg='#0a2145')
    title=tk.Entry(master,width=27,relief='flat')
    ribbon = tk.Canvas(master,width=width+1,highlightthickness=0, height=100, bg='#bfbfbf')
    name = tk.Label(master, text='Plotter', font=('Verdana',12,'bold'))
    name.config(background='#0a2145', foreground='white')   
    now = datetime.datetime.now()
    current_time = now.strftime("%H:%M:%S")
    label1=tk.Label(master,text=' '*225+'Sl.no'+' '*40+'Name'+' '*55+'Type'+' '*50+'Date'+' '*30+'Download path'
                    , bg='#1d1d30',fg='white',width=width)
    n = tk.StringVar() 
    graph = ttk.Combobox(master, width = 27, textvariable = n) 
    graph['values'] = ('Bar Graph','Frequency','Dot')
    plotter=tk.Button(master,text='Plot',command=lambda:graph_plot(graph.get()),relief='flat',bg='#e89802',
                      fg='white',width=10)
    add=tk.Button(master,text='+ Add',command=lambda:graph_plot(graph.get()),relief='flat',bg='#08a63f',
                      fg='white',width=10)
    date = tk.Label(master,text=current_time,fg='white',font=('Verdana'),bg='#1d1d30')
    path=tk.Entry(master,width=27)
    download=tk.Button(master,text=' Download',command=lambda:save(path.get()),relief='flat',bg='#0a91c7',
                      fg='white',width=10)
    
    canvas1.create_window(35, 20, window=name)
    canvas1.create_window(681, 90, window=ribbon)
    canvas1.create_window(681,20,window=navbar)
    canvas1.create_window(1320,20,window=plotter)
    canvas1.create_window(1230,20,window=add)
    canvas1.create_window(1140,20,window=download)
    canvas1.create_window(390,200,window=graph)
    canvas1.create_window(180,200,window=title)
    canvas1.create_window(40,150,window=label1)
    canvas1.create_window(560,200,window=date)
    canvas1.create_window(700,200,window=path)
    
canvas1 = tk.Canvas(master, width = width, height = height, bg = '#304566',highlightthickness=0,
                     relief='ridge')
canvas1.pack()
reload()
master.mainloop()
Code Carbonate
  • 640
  • 10
  • 18

1 Answers1

1

You need to add a line here:

canvas.create_image(0, 0, image=photo)   
canvas.pack()

do this instead:

canvas.create_image(0, 0, image=photo)   
canvas.image = photo
canvas.pack()

You need to keep reference to the photo or it gets garbage collected, see http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm for more information.

coderoftheday
  • 1,987
  • 4
  • 7
  • 21