首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tkinter:从对话框打开后显示图像

Tkinter:从对话框打开后显示图像
EN

Stack Overflow用户
提问于 2015-06-25 23:32:52
回答 2查看 1.9K关注 0票数 0

这是我使用Tkinter编写的第一个程序,所以如果我的问题有点幼稚,我提前道歉。

我有以下几点:

代码语言:javascript
复制
class Example(Frame):
def __init__(self, master=None):
    Frame.__init__(self,master)

    menubar = Menu(self)
    master.config(menu=menubar)
    self.centerWindow(master)
    self.Top_Bar(menubar,master)
    self.pack(fill = BOTH, expand = 1)

def Top_Bar(self,menubar,master):
    fileMenu = Menu(menubar,tearoff=False)
    menubar.add_cascade(label="File",menu=fileMenu)
    fileMenu.add_command(label="Open",command = self.open_file)
    fileMenu.add_command(label="Exit",command = self.quit)

    fileMenu = Menu(menubar,tearoff=False)
    menubar.add_cascade(label="Edit",menu=fileMenu)

    fileMenu = Menu(menubar,tearoff=False)
    menubar.add_cascade(label="Shortcuts",menu=fileMenu)

    fileMenu = Menu(menubar,tearoff=False)
    menubar.add_command(label="About",command = Message_About)

请注意,我将self.open_file作为命令,它本身就是一个函数:

代码语言:javascript
复制
def open_file(self):
    """ A function that opens the file dialog and allows a user to select a single file, displaying it on the page """
    global filename
    filename = []
    filename.append(str(unicodedata.normalize("NFKD",tkFileDialog.askopenfilename(filetypes=[("Astronomical Data","*.fit;*fits")])).encode("ascii","ignore")))

    for i in filename:
        stretch_type = "linear"
        image_manipulation_pyfits.create_png(i,stretch_type)
        x = Image.open("file.png")
        Label(image = x).pack()

我确信有一种更短、更有效的方法来编写这个函数,但这不是我目前的主要目标--它只是让一切正常工作。我的目标是将此图像x显示在Tkinter窗口中。它会给我一个错误

代码语言:javascript
复制
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
    return self.func(*args)
  File "tkinter1.py", line 125, in open_file
    Label(image = x).pack()
  File "C:\python27\lib\lib-tk\ttk.py", line 766, in __init__
    Widget.__init__(self, master, "ttk::label", kw)
  File "C:\python27\lib\lib-tk\ttk.py", line 564, in __init__
    Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "C:\python27\lib\lib-tk\Tkinter.py", line 2055, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
TclError: image specification must contain an odd number of elements

为清楚起见,之前的函数只获取一个输入.fits图像(从弹出的对话框中选择)并应用线性拉伸,然后将其作为.png图像保存到名称为"file.png“的同一目录中。

我已经用谷歌搜索了一天左右,没有找到任何关于这个错误的帖子。

EN

回答 2

Stack Overflow用户

发布于 2015-06-25 23:45:20

我找到的一个解决方案是:

代码语言:javascript
复制
        x = Image.open("file.gif")
        x = ImageTk.PhotoImage(x)
        label = Label(image = x)
        label.image = x
        label.pack()

  • 将图像保存为.gif并打开
  • 需要使用ImageTk中的PhotoImage。
票数 0
EN

Stack Overflow用户

发布于 2015-06-26 13:50:16

如果可以,我会发表评论,但只需注意@bjd2385的答案。在他的例子中,label.image = x保存了图像的引用,但是如果省略了这行代码,那么如果在设计模式中使用类,则需要使用self来保存引用。它应该是self.im = ImageTk.PhotoImage("..."),而不是im = ImageTk.PhotoImage("..."),否则它可以被垃圾回收,并且仍然显示图像的轮廓,但实际上没有图像。

此外,您还可以直接从PhotoImage调用中打开图像。我不确定可能使用的图像文件的完整限制,但我知道您可以使用.gif和.png。这是他为Python3.4重新设计的答案:

代码语言:javascript
复制
    import tkinter

    self.img = ImageTk.PhotoImage(file = "./images/file.png")
    label = Label(image = self.img)
    label.image = self.img # this line can be omitted if using the 'self' method to save a reference
    label.pack()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31054724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档