在tkinter中插入图片

时间:2019-04-24 15:32:04

标签: python image python-2.7 tkinter python-imaging-library

我正在尝试在tkinter GUI的画布上插入图像。我通常可以插入图像,但是在画布上插入图像时出现错误: “ pyimage不存在”或“找不到文件目录”

在“ PageMain”类中,我创建了一个小部件,并按照该线程上的答案-How to insert an image in a canvas item?

尽管如此,它仍然说尽管使用了答案仍找不到文件。

from Tkinter import *
import ttk 
import sqlite3
import Tkinter as tk
import Tkinter
import sys
import StringIO
from PIL import Image, ImageTk
import os



class MyApp(Tk):
    def __init__(self):
        Tk.__init__(self)
        container = ttk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        self.frames = {}
        for F in (PageMain, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='NSEW')
        self.show_frame(PageMain)


    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()



class PageMain(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.make_widget()

    def make_widget(self):
        canvas = Canvas(self, width="1000", height="600")

        img = PhotoImage(file='D:\plogo\logo.png')
        canvas.create_image(50, 10, image=img, anchor=NW)


         # demo button to change page
        btnChange = Button(canvas, text="Change", font="Arial 16",
                           command=lambda: self.controller.show_frame(PageOne),
                           bg="#a0ccda")
        btnChange .place(x=600, y=550, width="100", height="50")

        canvas.pack()



        def change_page(self):
            pass




if __name__ == '__main__':
    app = MyApp()
    app.title('Music Match')
    app.mainloop()

1 个答案:

答案 0 :(得分:0)

您必须保留对图像的引用,感谢Bryan,请参见下文,尤其是

  

w = tk.Canvas(self,)

     

w.img = tk.PhotoImage(file ='logo.png')

#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


class Main(ttk.Frame):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent
        self.init_ui()

    def init_ui(self):

        w = tk.Canvas(self, )
        w.img = tk.PhotoImage(file='logo.png')
        w.create_image(50,50, anchor=tk.N+tk.W, image=w.img)
        w.pack(expand=1)

    def on_close(self):
        self.parent.on_exit()

class App(tk.Tk):
    """Start here"""

    def __init__(self):
        super().__init__()

        self.protocol("WM_DELETE_WINDOW", self.on_exit)

        self.set_style()
        self.set_title()

        frame = Main(self,)
        frame.pack(fill=tk.BOTH, expand=1)

    def set_style(self):
        self.style = ttk.Style()
        #('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
        self.style.theme_use("clam")

    def set_title(self):
        s = "{0}".format('Simple App')
        self.title(s)

    def on_exit(self):
        """Close all"""
        if messagebox.askokcancel(self.title(), "Do you want to quit?", parent=self):
            self.destroy()               

if __name__ == '__main__':
    app = App()
    app.mainloop()

enter image description here