onlyforbopi
3/16/2017 - 10:59 AM

TKINTER

TKINTER

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 16 12:29:34 2017

@author: P.Doulgeridis
"""

import tkinter
import tkinter.filedialog

class myApplication:                     #1
    def __init__(self,root):
        self.root = root                 #2
        self.initialisation()            #3

    def initialisation(self):            #3
        tkinter.Label(self.root,text="Hello, world !").grid(column=0,row=0)   #4

def main(): 

    ## Initialize new tkinter application                             #5
    root = tkinter.Tk()
    
    ## Insert tile to tkinter application
    root.title('My application')
    #app = myApplication(root)
    #root.mainloop()
    
    # tkinter - directory chooser
    directory = tkinter.filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
    if len(directory) > 0:
        print ("You chose directory %s" % directory)
    
    # tkinter - file chooser    
    file = tkinter.filedialog.askopenfile(parent=root,mode='rb',title='Please select a file')
    if file != None:
        data = file.read()
        file.close()
        print ("I got %d bytes from the file." % len(data))

    

    
    # tkinter - file save as
    # we need to predetermine what types of file we can save it as.
    myFormats = [
    ('Windows Bitmap','*.bmp'),
    ('Portable Network Graphics','*.png'),
    ('JPEG / JFIF','*.jpg'),
    ('CompuServer GIF','*.gif'),
    ]
    
    filename = tkinter.filedialog.asksaveasfilename(parent=root,filetypes=myFormats,title="Save image as...")
    if len(filename) > 0:
        print ("Now saving as %s" % (filename))




if __name__ == "__main__":
    main()
    
    
    
###########################################################
###########################################################



#from Tkinter import *
# if you are working under Python 3, comment the previous line and comment out the following line
import tkinter as tk

root = tk.Tk()

w = tk.Label(root, text="Hello Tkinter!")
w.pack()

root.mainloop()