Python 2.7 Tk filedialog calling external programs
<p>I'm beginning to learn how to use python and need to develop some applications for a lab project.</p>

<p>I am trying to create a GUI to select a couple of files I want to pass as arguments to another program. The GUI part seems to work, but when I try to call the external script using Popen method but the variables allegedly captured through the GUI are not being passed to the Popen call.</p>

<p>I request your kind advice to solve this problem because it seems to be still a little beyond my current knowledge and I really need to use a Tk/GUI as interface for this project.</p>

<p>Here is my code: </p>

<pre><code>def button_fasta_callback():
    fasta = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a FASTA')
    if fasta != None:
        data_fasta = fasta.read()
        fasta.close()


def button_lista_callback():
    lista = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a list')
    if lista !=None:
        data_lista = lista.read()
        lista.close()

#####Create the buttons#######    
root = Tk()
root.title("Sequence extractor")

button_fasta = Button(root,
                       text="Choose FASTA",
                       command=button_fasta_callback)
button_fasta.pack(padx=150, pady=50)


button_lista = Button(root,
                       text="Choose a list",
                       command=button_lista_callback)
button_lista.pack(padx=150, pady=50)

entry = Entry(root, width=50)

root.mainloop()

caller = Popen(['C:\\Python_programs\\Seq_extractor.py', '-l', lista, '-f', fasta])
</code></pre>

<p>Thank you all!</p>

<p>Your <code>fasta</code> and <code>lista</code> variables are local to the <code>button_*</code> functions and are not visible where you try and call <code>Popen</code>. A quick fix would be to use <code>global</code> to make these visible as global variables. A better fix would be to refactor this use use a class and assign <code>self.fasta</code> and <code>self.lista</code> making these be member variables of the class instance.</p>

python tkinter tk