2016-11-25 50 views
1

嘿,我正在按照教程https://www.youtube.com/watch?v=a1Y5e-aGPQ4,我無法讓它正常工作。我想,當你按下菜單按鈕添加圖像:如何使圖像在窗口上彈出Tkinter python3

from tkinter import * 
from PIL import * 

class Window(Frame): 

    def __init__(self, master = None): 
     Frame.__init__(self, master) 

     self.master = master 

     self.init_Window() 

    def init_Window(self): 
     self.master.title("GUI") 

     self.pack(fill =BOTH, expand=1) 

     #quitButton = Button(self, text = "Quit", command =  self.client_exit) 
     #quitButton.place(x=0,y=0) 

     menu = Menu(self.master) 
     self.master.config(menu=menu) 

     file=Menu(menu) 
     file.add_command(label='Save',command= self.client_exit) 
     file.add_command(label='Exit',command= self.client_exit) 
     menu.add_cascade(label='File',menu=file) 

     edit = Menu(menu) 
     edit.add_command(label='Show Image', command=self.showImg) 
     edit.add_command(label='Show Text', command=self.showTxt) 
     menu.add_cascade(label='Edit',menu=edit) 

    def showImg(self): 
     load = Image.open('Pic.png') 
     render = ImageTk.PhotoImage(load) 

     img = Label(self, image=render) 
     img.image = render 
     img.place(x=0,y=0) 

    def showTxt(self): 
     text = Label(self,text='Hey') 
     text.pack 

    def client_exit(self): 
     exit() 



root = Tk() 
root.geometry("400x300") 
app = Window(root) 

root.mainloop() 

我試圖問學校周圍,StackOverflow的,和YouTube約3天,現在,一切都沒有解決我的問題,如果你需要有關它的更多信息請詢問。我收到錯誤代碼:

Exception in Tkinter callback 
Traceback (most recent call last): 
File "/usr/lib/python3.5/tkinter/__init__.py", line 1558, in __call__ 
return self.func(*args) 
File "/root/Desktop/Python Programs/Tkinter.py", line 35, in showImg 
load = Image.open('pic.png') 
AttributeError: type object 'Image' has no attribute 'open' 
+0

您使用'進口*'所以你不知道,如果你使用'tkinter.Image'或'PIL.Image' 。這就是爲什麼你不應該使用'import *' – furas

回答

0

您使用import *所以你不知道,如果你使用tkinter.ImagePIL.Image。這就是爲什麼你不應該使用import *

嘗試

from PIL import Image, ImageTk 
+0

cannont import name ImageTk,我可能沒有正確安裝PIL,我在Linux上這樣可能會出現問題... – PinkChicken

+0

然後使用'pip install pillow' – furas

+0

工作,得到了枕頭9.0.1,但仍然git的問題不能imort名稱'ImageTk' – PinkChicken

-1

圖像是一個有點棘手得到正確的,一些小技巧:保持圖像對象全局,避免垃圾收集,避免錯誤屬性(由閱讀文檔)。

在這個例子中,我不運用PIL和我打開一個gif圖片

#!/usr/bin/python 
#-*-coding:utf-8 -* 

#Python 3 
#must have a valid gif file "im.gif" in the same foldeer 

from tkinter import * 

Window=Tk() 
ListePhoto=list() 
ListePhoto.append(PhotoImage(file="im.gif")) 

def Try(): 


    Window.title('image') 
    Window.geometry('+0+0') 
    Window.configure(bg='white') 

    DisplayImage() 

def DisplayImage(): 

    label_frame=LabelFrame(Window, relief='ridge', borderwidth=12, text="AnImage", 
         font='Arial 16 bold',bg='lightblue',fg='black') 
    ListeBouttons=list()#Liste Vide pour les Radiobutton(s) 

    RadioButton = Radiobutton(label_frame,text="notext",image=ListePhoto[0], indicatoron=0) 
    RadioButton.grid(row=1,column=1) 
    label_frame.pack(side="left") 




if __name__ == '__main__': 
    Try()