【问题标题】:How to make image pop up on a window Tkinter python3如何使图像在窗口Tkinter python3上弹出
【发布时间】:2017-04-10 08:20:27
【问题描述】:

嘿,我正在学习一个教程,https://www.youtube.com/watch?v=a1Y5e-aGPQ4,但我无法让它正常工作。当您按下菜单按钮时,我正在尝试添加图像:

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'

【问题讨论】:

  • 你使用 import * 所以你不知道你是使用 tkinter.Image 还是 PIL.Image 。这就是为什么你不应该使用import *

标签: python python-3.x tkinter python-3.5


【解决方案1】:

你使用 import * 所以你不知道你是使用 tkinter.Image 还是 PIL.Image 。这就是为什么你不应该使用import *

试试

from PIL import Image, ImageTk

【讨论】:

  • 无法导入名称 ImageTk,我可能没有正确安装 PIL,我在 linux 上,所以这可能是个问题...
  • 然后使用pip install pillow
  • 工作了,得到了枕头 9.0.1,但仍然是 git 问题不能以'ImageTk'名字命名
  • 显示有问题的完整错误信息。不要使用Tkinter 作为您的文件名,因为import 可能会尝试导入您的文件Tkinter 而不是python 模块Tkinter
  • Traceback(最近一次调用最后一次):文件“/root/Desktop/Python Programs/Tkinter.py”,第 2 行,在 from PIL import Image, ImageTk ImportError: cannot import name ' ImageTk' >>>
【解决方案2】:

要正确处理图像有点棘手,一些提示:保持图像对象全局,避免垃圾收集,避免属性错误(通过阅读文档)。

在这个例子中,我没有使用 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2018-04-28
    • 2010-12-01
    • 1970-01-01
    • 2021-03-07
    相关资源
    最近更新 更多