Good afternoon all,
Rather than using the command line or hard coding a file name in Python, I'd like to have a separate class that I can use in any other Python program that allows a file to be chosen with the Tkinter file dialog box. Here is what I have so far:
import Tkinter as tk
import tkFileDialog
import tkFont
###################################################################################################
class MyFileDialog(tk.Frame):
# class level variables #######################################################################
my_form = tk.Tk() # declare form
my_button = tk.Button(my_form) # declare button
chosen_file = ""
# constructor #################################################################################
def __init__(self):
# create button event
self.my_button.configure(text = "press here", command = self.on_my_button_click)
self.my_button.pack() # add button to form
# make the button font bigger so I don't have to squint
updated_font = tkFont.Font(font = self.my_button['font'])
updated_font['size'] = int(float(updated_font['size']) * 1.6)
self.my_button.configure(font = updated_font)
###############################################################################################
def on_my_button_click(self):
self.chosen_file = tkFileDialog.askopenfilename() # get chosen file name
return
###############################################################################################
def get_chosen_file(self):
return self.chosen_file # return chosen file name
###################################################################################################
if __name__ == "__main__":
my_file_dialog = MyFileDialog()
my_file_dialog.my_form.mainloop()
# I need to get the chosen file here, when the file is chosen, NOT when the window is closed !!
# please help !!
print my_file_dialog.get_chosen_file() # this does not print until the window is closed !!!
The current problem with this code is (as the comment towards the end indicates) that the test code does not receive the chosen file name until the window is closed, however I need the test code to receive the file name when the file is chosen.
I should mention that in production use the test code would be in a different file altogether that would import and instantiate this class, therefore the modularity of the class should be maintained. Anybody out there have a solution? Please help !