我想构建一个程序来拾取图像并将其显示在画布上。它不会在画布上显示图像。我声明了一张画布“可以查看所拾取图像的照片,但是它不起作用。我对Tkinter的了解不多。
from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
from tkinter import filedialog
import os
#creating the application main window.
app = Tk()
app.title("Multi-Language OCR")
app.geometry("1200x600")
def open_image():
global imagefile
app.filename = filedialog.askopenfilename(initialdir="C:",title="Open image",filetypes=(("png files","*.png"),("all files","*.*")))
img = ImageTk.PhotoImage(Image.open(app.filename))
photo.create_image(0, 0, anchor=NW, image=img)
#button defination
button_0 = Button(app,text="Open Image",bg="black",fg="white",command=open_image)
button_1 = Button(app, text="Bengali",bg="black",fg="white",width = "15")
button_2 = Button(app, text="Gujrati",bg="black",fg="white",width = "15")
button_3 = Button(app, text="Hindi",bg="black",fg="white",width = "15")
button_4 = Button(app, text="Kannada",bg="black",fg="white",width = "15")
button_5 = Button(app, text="Malyalam",bg="black",fg="white",width = "15")
button_6 = Button(app, text="Marathi",bg="black",fg="white",width = "15")
button_7 = Button(app, text="Nepali",bg="black",fg="white",width = "15")
button_8 = Button(app, text="Punjabi",bg="black",fg="white",width = "15")
button_9 = Button(app, text="Sanskrit",bg="black",fg="white",width = "15")
button_10 = Button(app, text="Sindhi",bg="black",fg="white",width = "15")
button_11 = Button(app, text="Tamil",bg="black",fg="white",width = "15")
#canvas used to display image
photo = Canvas(app,bg="black",width="400",height="550")
#label used to print output text;
output = Label(app,text="OCR text",bg="cyan",fg="white",)
#grid
button_0.grid(row=0,column=0,padx="5" )
photo.grid(row ="1",column="0",rowspan=11,padx="20")
button_1.grid(row=1 ,column =1,padx=50)
button_2.grid(row=2 ,column=1)
button_3.grid(row=3 ,column = 1)
button_4.grid(row=4 ,column = 1)
button_5.grid(row=5 ,column = 1)
button_6.grid(row=6 ,column = 1)
button_7.grid(row=7 ,column = 1)
button_8.grid(row=8 ,column = 1)
button_9.grid(row=9 ,column = 1)
button_10.grid(row=10 ,column = 1)
button_11.grid(row=11 ,column = 1)
output.grid(row=2,column = 2,rowspan=11)
#Entering the event main loop
app.mainloop()