命令行脚本的 Cookbook GUI 界面 [英] Cookbook GUI interface for a command-line script

查看:46
本文介绍了命令行脚本的 Cookbook GUI 界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令行 Python 脚本,可以很好地将一种文件转换为另一种给定几个参数,现在想将它部署给我的一些可能不知道命令行是什么的同事.

I have a command-line Python script that works well to convert one sort of file into another given a few parameters and would now like to deploy this to some of my colleagues who may not know what a command line is.

我可以花上几个小时来确定哪个 Python GUI 工具包是最好的",然后学习如何做我需要的事情,但似乎以前应该这样做.

I could work for hours trying to determine which Python GUI toolkit is "best", then learning how to do what I need, but it seems like this would have been done before.

是否有一种相对简单的方法来 GUI 化我的程序?指导某种课程/教程或现有的、有记录的、简洁的程序会很好.

Is there a relatively cook-book method to GUIify my program? Direction to either some sort of lesson/tutorial or an existing, documented, concise program would be excellent.

推荐答案

只是一个快速而简单的例子,它可以教会你足够的 wxPython 来开始:

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

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, following code runs command line in another thread and shows 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()

这篇关于命令行脚本的 Cookbook GUI 界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆