1

I found this code online that open a file and show it is tk window. But it only allows me to open the png file. May I know how can I modify the code to open jpg and pdf file, or other file types?

import tkinter
import tkinter.filedialog

root = tkinter.Tk()


def display_image():
    f = tkinter.filedialog.askopenfilename(
        parent=root, initialdir='C:/Tutorial',
        title='Choose file'
    )

    new_window = tkinter.Toplevel(root)

    image = tkinter.PhotoImage(file=f)
    l1 = tkinter.Label(new_window, image=image)
    l1.image = image
    l1.pack()


b1 = tkinter.Button(root, text='Display image', command=display_image)
b1.pack(fill='x')

root.mainloop()
Danni Peng
  • 75
  • 5
  • You need install PIL (pillow) for other image types – patel Dec 26 '18 at 08:40
  • Use the `filetype=` parameter as shown in [filedialog-tkinter-and-opening-files](https://stackoverflow.com/questions/9239514/filedialog-tkinter-and-opening-files) – stovfl Dec 26 '18 at 11:06

0 Answers0