按钮显示白色画布而不是图像

时间:2015-06-11 18:31:37

标签: python tkinter

我试图制作一张图片,但它无法正常工作,我不知道自己做错了什么。当我点击应该显示图像的按钮时,我得到的只是白色画布,没有图像。如何让它显示?

def show_original(self):

    from os.path import exists
    from PIL import Image, ImageTk


    if not os.path.exists(self.wdg_orig_file_name_.get()):
        tkMessageBox.showinfo('Load','File does not exist:' + self.wdg_orig_file_name_.get())

        return
    self.orig_image_=Image.open(str_orig_file_name)
    canvas = self.gui_.ca(500,500,bg='white')
    im_TK = ImageTk.PhotoImage(self.orig_image_)
    canvas.create_image(250,250,image=im_TK)
    canvas.pack()
    pass
主循环中的

self.wdg_orig_file_name_.get()是:

 self.wdg_orig_file_name_ = self.gui_.en(text='boat.png')

全球str_orig_file_name已在pick_file()中分配:

def pick_file(self):
    '''Opens a file dialog and sets its result to the filename entry'''
    global str_orig_file_name
    str_orig_file_name = tkFileDialog.askopenfilename()
    if str_orig_file_name:
        self.wdg_orig_file_name_.delete(0, END)
        self.wdg_orig_file_name_.insert(0, str_orig_file_name)

        #We got a new image to process. Forget the previous results.
        self.orig_image_ = None
        self.preview_image_ = None

2 个答案:

答案 0 :(得分:-1)

你试过easygui吗?

#!/usr/bin/python

from easygui import *

image = "img.jpg"
msg   = "Do you like this picture?"
choices = ["Yes","No","No opinion"]
reply=buttonbox(msg,image=image,choices=choices)

很简单。

http://www.ferg.org/easygui/tutorial.html

答案 1 :(得分:-1)

对不起我虽然OP正在寻找一种显示图像的方法,现在我看到图像应该在按钮上。好吧,我试过这个,它与.gif和.png一起工作得很好。但是,.jpg会出错:“_ tkinter.TclError:无法识别图像文件中的数据”img.jpg“”。

#!/usr/bin/python

from Tkinter import *
root=Tk()
b=Button(root,justify = LEFT)
photo=PhotoImage(file="img.png")
b.config(image=photo,width="100",height="100")
b.pack(side=LEFT)
root.mainloop()