Cookbook GUI interface for a command-line script Cookbook GUI interface for a command-line script tkinter tkinter

Cookbook GUI interface for a command-line script


Just a quick and simple example which may teach you enough wxPython to get going:

import wxfrom subprocess import Popen, PIPEclass MainWindow(wx.Frame):    def __init__(self, *args, **kwargs):        wx.Frame.__init__(self, *args, **kwargs)        self.panel = wx.Panel(self)        self.button = wx.Button(self.panel, label="Run!")        self.command = wx.TextCtrl(self.panel)        self.result = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)        self.sizer = wx.BoxSizer(wx.VERTICAL)        self.sizer.Add(self.command, 0, wx.EXPAND)        self.sizer.Add(self.button, 0, wx.EXPAND)        self.sizer.Add(self.result, 1, wx.EXPAND)        self.command.SetValue("dir")        self.button.Bind(wx.EVT_BUTTON, self.CallCommand)        self.panel.SetSizerAndFit(self.sizer)          self.Show()    def CallCommand(self, e):        p = Popen(self.command.GetValue(), shell=True,                   stdin=PIPE, stdout=PIPE, stderr=PIPE)        r = p.communicate()        self.result.SetValue(r[0])app = wx.App(False)win = MainWindow(None)app.MainLoop()

To complete the example, following code runs command line in another thread and shows the result line by line:

import wxfrom wx.lib.delayedresult import startWorker from subprocess import Popen, PIPEclass MainWindow(wx.Frame):    def __init__(self, *args, **kwargs):        wx.Frame.__init__(self, *args, **kwargs)        self.panel = wx.Panel(self)        self.button = wx.Button(self.panel, label="Run!")        self.command = wx.TextCtrl(self.panel)        self.result = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)        self.sizer = wx.BoxSizer(wx.VERTICAL)        self.sizer.Add(self.command, 0, wx.EXPAND)        self.sizer.Add(self.button, 0, wx.EXPAND)        self.sizer.Add(self.result, 1, wx.EXPAND)        self.command.SetValue("dir")        self.button.Bind(wx.EVT_BUTTON, self.CallCommand)        self.panel.SetSizerAndFit(self.sizer)          self.Show()    def CallCommand(self, e):        startWorker(self.WorkCommandDone, self.WorkCommand)    def WorkCommand(self):        self.button.Disable()        p = Popen(self.command.GetValue(), shell=True,                   stdin=PIPE, stdout=PIPE, stderr=PIPE)        while True:            line = p.stdout.readline()            if line != '':                wx.CallAfter(self.result.AppendText, line)            else:                break    def WorkCommandDone(self, result):        self.button.Enable()app = wx.App(False)win = MainWindow(None)app.MainLoop()


There are a few answers advocating wxpython. However, any toolkit will work for this project. Tkinter has the added benefit tha you and your collegues already have it installed and it is very easy to use.

That being said, the other toolkits are more-or-less equally easy to use but you might have to jump through a hoop or two to get them installed. If installation is not an issue it won't matter which one you pick.

Unfortunately, telling you how to "GUIfy" your program is hard since we know nothing about your app. Probably all it will involve is putting up a few labels and input widgets, then creating a button that collects all the data and then runs your program with the subprocess module.


Basically you just need to figure out what widgets will hold the data you want the best. I suspect you could use a couple combo boxes to hold different sets of extensions. Or you could just use the path name strings to figure that out. Hit a button and run the conversion process, probably in another thread so the GUI remains responsive.

I'm biased for wxPython. However, you're better off taking a look at their demos and documentation and seeing which GUI toolkit fits your brain the easiest.