Jack_of_All_Trades
Jack_of_All_Trades

Reputation: 11468

Converting existing command-line script to GUI in python

This might seem as silly question but I am thinking there might be the simple solution for this problem. I have a python script which does some mathematical calculation which is command-line-based. I don't want to make any major modification to the code which I have right now. What should be my approach now to create a GUI ( I use wxpython) which uses that script as its model while leaving model to function as stand-alone python script as it is right now. I know it is the question of inheritance and polymorphism but I am not clear about the approach I should take. Any guidelines will be greatly appreciated.

Upvotes: 3

Views: 2287

Answers (2)

James Thiele
James Thiele

Reputation: 413

Let's say your command line executable is:

% compute -flag1 -flag2 datafile outputfile

In WxPython you could use check boxes to choose whether flag1 and/or flag2 are used, a file chooser widget for datafile, a text input field for outputfile and 'execute' button. When execute is clicked create a command line in a string and pass it to one of the methods in the subprocess module.

Upvotes: 2

Fenikso
Fenikso

Reputation: 9451

It depends on the quality of your command line tool. Basically if it is written "correctly", it encapsulates its functionality in some functions or classes which are reusable in other scripts - meaning you can import and use them without problem. The "main" portion of the command line tool then parses command line arguments and calls those functions / classes. It that all is true then you can, obviously, import your functionality in GUI based app and use it.

So in short your functionality has to be separated from user interface and then switching the interface is more or less trivial. This is usually called separating front end and back end.

Upvotes: 2

Related Questions