GUI GUI for command line script

I have a Python command line script that works well to convert one kind of file to another given several parameters and would now like to expand this for some of my colleagues who may not know what a command line is.

I could spend hours trying to figure out which Python GUI tool is "best" and then learn to do what I need to do, but it looks like it would have been done before.

Is there a cookbook way to GUIify my program? A referral to any lesson / tutorial or an existing, documented, concise curriculum would be excellent.

+3


source to share


4 answers


Just a quick and simple example that can teach you enough wxPython to navigate:

import wx
from subprocess import Popen, PIPE

class 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, the following code runs a command line on a different thread and displays the result line by line:

import wx
from wx.lib.delayedresult import startWorker 
from subprocess import Popen, PIPE

class 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()

      

+4


source


There are several answers out there protecting wxpython. However, any toolkit will work for this project. Tkinter has an added benefit that you and your colleagues have already installed, and it's very easy to use.

That being said, other tools are more or less equally easy to use, but you might have to go through a hoop or two to get them set up. If installation isn't a problem, it doesn't matter which one you choose.



Unfortunately, telling you how "GUIfy" your program is, is hard because we don't know anything about your application. This will likely all involve a few shortcuts and input widgets and then creating a button that collects all the data and then runs your program using a subprocess module.

+1


source


Basically, you just need to figure out which widgets will contain the data you want. I suspect you might be using multiple combo boxes to store different sets of extensions. Or you can just use pathname strings to figure it out. Click the button and start the conversion process, possibly on a different thread to keep the GUI responsive.

I am tied to wxPython. That said, you'd better take a look at their demos and documentation and see which set of GUI tools works best for your brain.

0


source


It mainly depends on your needs. If your need is simple, you can just go with tkinter, which is bundled with python itself. If you use this, you will not rely on a third party library to implement your GUI. Since you want to make this available to your colleagues, it might be easier to compile with py2exe or other similar stuff to an exe, which can be tricky if you are using a third party GUI library. However, if you want to add more functionality to your GUI, wxpython / pyqt / pyGTK is a GUI search toolkit. Personally, I prefer wxpython due to its cross-platform nature, but pyqt and pyGTK are equally as good as I've heard.

0


source







All Articles