簡體   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