繁体   English   中英

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

[英]Tkinter: Display image after opening from dialog

这是我使用Tkinter编写的第一个程序,因此,如果我的问题有点天真,我事先表示歉意。

我有以下内容:

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作为命令,它本身就是一个函数:

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窗口中。 它给我错误

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”的同一目录中。

我过去一天左右的时间都在Google上搜索了,但找不到该错误的任何线索。

我发现一种解决方案:

        x = Image.open("file.gif")
        x = ImageTk.PhotoImage(x)
        label = Label(image = x)
        label.image = x
        label.pack()
  • 将图像另存为.gif并打开
  • 需要使用ImageTk中的PhotoImage。

如果可以的话,我会发表评论,不过只是关于@ bjd2385答案的注释。 在他的情况下, label.image = x保存图像的引用,但是,如果省略此行代码,那么如果在设计模式中使用类,则需要使用self保存引用。 而不是im = ImageTk.PhotoImage("...")而是self.im = ImageTk.PhotoImage("...")否则它会被垃圾收集并仍然显示图像的轮廓,但实际上没有图像。

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

    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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM