Execute external program in Tk Python 2.7
0
0
Entering edit mode
8.3 years ago
uxue33 ▴ 20

Hello I'm beginning to learn how to use python to develop some tasks in the lab.

I'm trying to learn how to use the Tk module in Python 2.7 but can't figure out how to execute an external program and make it work.

Here is my code:

import sys
from Tkinter import *
import tkFileDialog
from tkFileDialog import askopenfilename
import os

fasta= ""
lista = ""
window = Tk()
window.title("Sequence extractor")

def callback():
    fasta= askopenfilename()
    lista = askopenfilename()

    print fasta
    print lista
    return fasta,lista
errmsg = 'Error!'
but_fas = Button(text='FASTA Open', command=callback).pack(fill=X)
but_lis = Button(text='Lista Open', command=callback).pack(fill=X)

window.mainloop()

call = ("c:\Python_programs\Seq_extractor.py -l"+ lista+"-f " + fasta)
print call

os.system (call)

Is it possible that there is a problem with the order of the commands?

python tk tkinter • 5.2k views
ADD COMMENT
0
Entering edit mode

Is Seq_extractor.py your external script you want to execute? Not sure about use in Tkinter, but os.system is not the prefered method. You should have a look at the subprocess module.

ADD REPLY
0
Entering edit mode

Yes, is Seq_extractor.py.

I have looked for the subprocess module but does not work.

import sys
from Tkinter import *
import tkFileDialog
from tkFileDialog import askopenfilename
import subprocess
from subprocess import Popen
import os

fasta= ""
lista = ""
window = Tk()
window.title("Sequence extractor")

def callback():
    fasta= askopenfilename()
    lista = askopenfilename()

    print fasta
    print lista
    return fasta,lista

errmsg = 'Error!'
but_fas = Button(text='FASTA Open', command=callback).pack(fill=X)
but_lis = Button(text='Lista Open', command=callback).pack(fill=X)

subprocess.call(['c:\Python_programs\Seq_extractor.py', '-l', lista, '-f', fasta])

b = Button(text = 'execute!', command=callback ).pack(fill=X)

How about using execfile()?

ADD REPLY
1
Entering edit mode

Oh and the worst thing you can say is "it doesn't work". Tell us what doesn't work, the error message,...

ADD REPLY
0
Entering edit mode

I'm so sorry, I will take it into acount for future questions.

Here is the error:

Traceback (most recent call last):
  File "C:\Python_programs\prueba.py", line 26, in <module>
    subprocess.call(['c:\Python_programs\Seq_extractor.py', '-l', lista, '-f',
fasta])
  File "C:\Python27\lib\subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
    startupinfo)
WindowsError: [Error 193] %1 no es una aplicaci¾n Win32 vßlida
ADD REPLY
0
Entering edit mode

subprocess.call() does work, use the shlex module to properly format your arguments subprocess.call(shlex.split(call))

ADD REPLY

Login before adding your answer.

Traffic: 1544 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6