Skip to content

Instantly share code, notes, and snippets.

@daniel-woods
Last active August 29, 2015 14:19
Show Gist options
  • Save daniel-woods/3830f342638f2a5dc754 to your computer and use it in GitHub Desktop.
Save daniel-woods/3830f342638f2a5dc754 to your computer and use it in GitHub Desktop.
#Open an Image File and display some simple metadata.
import os # os.stat
from tkinter import *
import time # for getting time
from tkinter import filedialog
from tkinter import messagebox
from tkinter import ttk
class Gui:
def __init__(self, parent):
self.parent = parent
self.parent.title("Display the file metadata.")
# Make protocol handler manage interaction between the program and the window
parent.protocol("WM_DELETE_WINDOW", self.catch_destroy)
frame = Frame(self.parent, padx="5m", pady="5m")
frame.grid(row=0, column=0)
self.menu = Menu(frame)
self.file_menu = Menu(self.menu)
self.file_menu.add_command(label="Open", command=self.open_file)
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=self.catch_destroy)
self.menu.add_cascade(label="File", menu=self.file_menu)
self.parent.config(menu=self.menu)
self.filename = StringVar()
self.filename.set("FILENAME")
self.filesize = StringVar()
self.filesize.set("FILESIZE")
self.createdate = StringVar()
self.createdate.set("CREATE DATE")
label = Label(frame, textvariable=self.filename)
label.grid(row=0, column=0)
label = Label(frame, textvariable=self.filesize)
label.grid(row=1, column=0)
label = Label(frame, textvariable=self.createdate)
label.grid(row=2, column=0)
def catch_destroy(self):
if messagebox.askokcancel("Quit", "Are you sure?"):
self.parent.destroy()
def open_file(self):
my_file = filedialog.askopenfilename(filetypes=(("JPEG", "*.jpg; *.jpeg"), ("PNG", "*.png"),
("TIFF", "*.tif; *.tiff"), ("SVG", ".svg"),
("All Files", "*.*")))
if my_file:
self.filename.set("FILE: {}".format(my_file))
self.filesize.set("SIZE: {0:.3f} KB".format(os.stat(my_file).st_size/1024))
my_time = time.asctime(time.localtime(os.stat(my_file).st_ctime))
self.createdate.set("CREATED: {}".format(my_time))
def main():
root = Tk()
Gui(root)
root.mainloop()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment