0

使用 wx python 为 GUI 编写了脚本。我只需要加载另一个 python 文件并运行使用 GUI 为 python 文件的变量提供输入(值)的选项。并显示python文件结果。我能够加载文件需要运行....

4

1 回答 1

0

这是我的GooeyPi 应用程序中的一些代码。它是 PyInstaller 的 GUI 前端。它pyinstaller.py使用一些标志运行代码并将输出发送到textctrl小部件。它用于wx.Yield避免锁定接口,尽管将代码移动到自己的线程是另一种选择。

class GooeyPi(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(GooeyPi, self).__init__(*args, **kwargs)
        ...
        # Results go in here. Read only style so people can't write in it.
        self.txtresults = wx.TextCtrl(self.panel, size=(420,200),
                                      style=wx.TE_MULTILINE|wx.TE_READONLY)


    def OnSubmit(self, e):
        ...
        # getflags returns something like ['python.exe', 'pyinstaller.py' '--someflag' '--someotherflag']
        flags = util.getflags(self.fbb.GetValue())
        logging.debug('calling subprocess {}'.format(flags))
        for line in self.CallInstaller(flags):
            self.txtresults.AppendText(line)               

    def CallInstaller(self, flags):
        ' Generator function, yields on every line in output of command 
        p = subprocess.Popen(flags, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        while(True):
            retcode = p.poll()
            line = p.stdout.readline()
            wx.Yield() # Prevent GUI from freezing
            yield line
            if(retcode is not None):
                yield ("Pyinstaller returned return code: {}".format(retcode))
                break

和我的 getflags 函数供参考。它用于sys.executable查找 python 二进制文件os.path.join的路径和查找要运行的 python 脚本的路径。然后它会根据我的配置文件附加一些标志。然后返回结果列表。

def getflags(fname):
    flags=[]
    flags.append(sys.executable) # Python executable to run pyinstaller
    flags.append(os.path.join(config['pyidir'], 'pyinstaller.py'))
    if config['noconfirm']:
        flags.append('--noconfirm')
    if config['singlefile']:
        flags.append('--onefile')
    if config['ascii']:
        flags.append('--ascii')
    if config['windowed']:
        flags.append('--noconsole')
    if config['upxdir'] != '':
        flags.append('--upx-dir=' + config['upxdir'])
    if pyversion(config['pyidir']) == 2.1:
        flags.append('--distpath=' + os.path.dirname(fname))
    else:
        flags.append('--out=' + os.path.dirname(fname))
    flags.append(fname)
    return(flags)
于 2013-08-26T12:54:58.550 回答