【发布时间】:2020-02-19 06:15:22
【问题描述】:
我正在尝试让图像显示在 tkinter 画布中。
我知道Canvas 可以处理形状和文本,但无法使用图像。
我正在使用PILImageTK.PhotoImage,但它也无法使用“.ppm”图像创建Tkinter.PhotoImage。
'png' 图像与 python 文件存储在同一目录中。
import tkinter as tk
from PIL import Image, ImageTk
class Window:
def __init__(self):
self.window = tk.Tk()
self.window.title("COMP")
self.window.geometry("1200x600")
topframe = tk.Frame(self.window, highlightbackground='black', highlightthickness=1)
topframe.pack(side='top')
self.noteview = NoteView(topframe, self.songString)
class NoteView:
def __init__(self, frame):
self.canvas = tk.Canvas(frame, width=60, height=200)
self.canvas.pack()
self.canvas.create_text(15, 190, text='hi')
im = Image.open('png.png')
self.image = ImageTk.PhotoImage(im)
self.canvas.create_image(20, 20, anchor='nw', image=self.image)
w = Window()
TypeError: 'PhotoImage' 对象不可调用
【问题讨论】:
-
试试
self.panel = Label(root, image = im) self.panel.pack(side = "bottom", fill = "both", expand = "yes") -
@learner 不幸的是,我需要在画布上完成,所以我可以在它下面分层。
-
对此感到抱歉。你能指出哪一行给出了这个错误吗?
标签: python image tkinter tkinter-canvas