Pierre Dudek
Pierre Dudek

Reputation: 262

tkinter - ask the user for files directory and then run a script

I'm new to Python and I really need help on this, I've been looking everywhere but i can't find an answer.

I created a script that compares data from 2 Excels files and write a report in a new file. It's a long script that includes a lot of functions and objects.

Now I want to make this script usefull to anyone in my workteam and create a GUI. I am using Tkinter. I have created 2 buttons to ask the user for the files directory and store it in a list. I'd like my original script to use the two inputs and run when a third button is clicked on.

How should I include my script in the tkinder app script?? Here is my app:

from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog

listfile = []

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):

        button = Tkinter.Button(self,text=u"data 1",command=self.OnButtonClick)
        button2 = Tkinter.Button(self,text=u"data 2",command=self.OnButtonClick)
        #here is the third button to run the script, the command isn't right
        button3 = Tkinter.Button(self,text=u"Run script",command=self.OnButtonClick)
        button.pack(side='bottom',padx=15,pady=15) 
        button2.pack(side='bottom',padx=15,pady=15)
        button3.pack(side='bottom',padx=15,pady=15) 

    def OnButtonClick(self):
        x = tkFileDialog.askopenfilename(title='Please select data directory')
        listfile.append(x)

    # def OnButtonClick2(self):
        # Can I run my original script here?
        # I read one should not include a function into a function

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

Upvotes: 1

Views: 6605

Answers (3)

PM 2Ring
PM 2Ring

Reputation: 55469

Let's say that your original script is named excel_report.py and it's located in the same directory as your Tkinter script. I'm assuming that excel_report.py is properly organised so that all the processing happens in functions, and that it has a main() function that gathers filenames from the command line and then calls the function that does the actual work,

eg make_report(excelfilename1, excelfilename2, reportname).

And I also assume that excel_report.py ends with

if __name__ == "__main__":
    main()

If all those things are true, then in your Tkinter script you can just put

from excel_report import make_report 

up near the top of the script,

and then put the call to make_report() in your your callback function for the 3rd button (eg)

def OnButtonClick3(self):
    make_report(self.name1, self.name2, self.reportname)

Where self.name1, self.name2, and self.reportname contain the file names that you've gathered using the Tkinter file dialogs.

Upvotes: 2

jaredobr
jaredobr

Reputation: 28

The GUI might be overkill for something like this if all you need them to do is select two directories and have the script run. If you want to make it really simple, I'd suggest ditching the UI and have your script to the following:

  1. Ask user to select Dir 1
  2. Ask user to select Dir 2
  3. Automatically run script after Dir 1 and Dir 2 have been selected

You can do this by using tkFileDialog.askdirectory() as mentioned by patthoyts. Simplified version:

import tkFileDialog

def your_script(dir1, dir2):
    *your script goes here with dir1 and dir2 as inputs for processing*

dir1 = tkFileDialog.askdirectory()  
dir2 = tkFileDialog.askdirectory()

your_script(dir1,dir2)

If you want to make it even easier for the end-user, look into converting your script to an app (if they use Mac) or exe (if they use windows). py2app or py2exe will do the trick. Mac comes with python pre-installed while Windows does not, so your team wouldn't be able to run your script period if they are on Windows without python. the py2whatever modules can generate an executable that has python pre-packaged so someone that doesn't have python on their machine can still run your script.

Upvotes: 0

patthoyts
patthoyts

Reputation: 33203

Tk provides a function tk_chooseDirectory that launches either a system provided directory chooser on Windows and I believe MacOSX or a suitable dialog otherwise. On Tkinter this is exposed using the filedialog namespace eg:

from tkinter import filedialog
filedialog.askdirectory()

See TkDocs for some examples (search for chooseDirectory).

If what you are asking is actually about executing python script from within the interpreter then this has been asked at How to execute a file within the python interpreter? with Python3 additions given at What is an alternative to execfile in Python 3.0?.

Upvotes: 1

Related Questions