【问题标题】:PIL and Tkinter, multi windowsPIL 和 Tkinter,多窗口
【发布时间】:2011-11-13 13:08:46
【问题描述】:

我收到此消息:图像“pyimage2”不存在。我想要多窗口的图片,怎么做?

这是我的代码:

import Image
import ImageTk
import Tkinter



def new():
    wind = Tkinter.Tk()
    wind.geometry('600x600')               # This not work, why? 
    imageFile2 = Image.open("someimage2.jpg")
    image2 = ImageTk.PhotoImage(imageFile2)

    panel2 = Tkinter.Label(wind , image=image2)
    panel2.place(relx=0.0, rely=0.0)
    wind.mainloop()

master = Tkinter.Tk()
master.geometry('600x600')               # This work fine
imageFile = Image.open("someimage.jpg")
image1 = ImageTk.PhotoImage(imageFile)

panel1 = Tkinter.Label(master , image=image1)
panel1.place(relx=0.0, rely=0.0)
B = Tkinter.Button(master, text = 'New image', command = new).pack()
master.mainloop()

【问题讨论】:

    标签: python tkinter python-imaging-library


    【解决方案1】:

    wind = Tkinter.Tk() 更改为wind = Tkinter.Toplevel()

    def new():
        wind = Tkinter.Toplevel()
        wind.geometry('600x600')       
    

    这就是你需要改变的全部。


    参考:

    【讨论】:

    • Niceeeee unutbu,你每次都帮我,谢谢!
    • 这个答案对我也有帮助。但是,我现在看到在我的程序顶部框架旁边弹出了一个额外的小空框架。关于如何摆脱这种情况的任何想法?
    • @elfonso:这个问题是关于如何创建两个单独的窗口。您看到的“小空框”可能是其中没有小部件的窗口之一。如果你只想要一个窗口,那么你的程序应该调用root = Tkinter.Tk(),而不是调用Tkinter.Toplevel()
    • 但我使用您的解决方案的原因是使用root = Tkinter.Tk() 并没有让我显示图像。我得到了image "pyimage2" doesn't exist,就像OP一样。有没有办法在不创建不需要的窗口的情况下解决该错误?
    • 您是否多次致电Tkinter.Tk()?如果是这样,那就是问题所在。有关如何显示图像的示例,请参阅this page。如果这不能解决问题,请在您的代码中发布一个新问题。
    猜你喜欢
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多