0

So I'm working on a project, and I decided to use tkinter to make a multipage GUI, and I need it to display image from 2d-array, but it's always empty, so what can I do? I have a load button to get the file path from a input box and use other function to get the data, that part is fine but I followed the tutorials online by using 'ImageTk.PhotoImage(image)' to load the image data but it is not visible in canvas.

Thank you very much.

import os
from tkinter import *
from tkinter import filedialog

from PIL import Image, ImageTk


def show_frame(frame):
   frame.tkraise()
 
window = Tk()
window.geometry("1680x997")
 
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
 
 
StartPage = Frame(window)
Page1 = Frame(window)
 
 
for frame in (StartPage, Page1):
   frame.grid(row=0, column=0, sticky='nsew')
 
show_frame(StartPage)
 
# ======================================== StartPage ========================================
 
label = Label(StartPage, text ="Start Page")
label.pack()
 
 
button1 = Button(StartPage, text ="Page1 function",
           command = lambda : show_frame(Page1)
)
button1.place(anchor=CENTER, relx=0.2, rely=0.4)
 
# ======================================== Page1 ========================================
def openfile(entryBox):
   filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file", filetypes=(("all files", "*.*")))
   entryBox.delete(0, END)
   entryBox.insert(END, filename)
  
def load_image(entryBox):
   filename = entryBox.get()
   array = read_file(filename)


   image=Image.fromarray(array)

   img = ImageTk.PhotoImage(image)
 
   canvas = Canvas(Page1, width=1100, height=830, bg='white')
   canvas.place(anchor=CENTER, relx=0.5, rely=0.5)
   canvas.create_image((int(canvas_width/2),int(canvas_height/2)), anchor=CENTER, image=img)
 
 
fileEntry = Entry(Page1, width=int(100))
fileEntry.place(anchor=CENTER, relx=0.5, y = 56)
 
return_btn = Button(Page1, text ="Home", command = lambda : show_frame(StartPage), width=7)
return_btn.place(anchor=CENTER, relx=0.1, y=54)
 
selectButton = Button(Page1, text="Select file", command=lambda: openfile(fileEntry), width=7)
selectButton.place(anchor=CENTER, relx=0.9, y=54)
 
load_btn = Button(Page1, text ="Load", command = lambda:load_image(fileEntry), width=7)
load_btn.place(anchor=CENTER, relx=0.9, y=100)
 
 
window.mainloop()



enter image description here

TheEd0315
  • 13
  • 4

0 Answers0