如何在python中的另一个应用程序中使用函数返回的值?

时间:2018-05-14 19:21:02

标签: python python-3.x return wxpython

我想知道如何从python中开发的可执行程序中返回一个值,该值将由另一个应用程序解释。另外,根据按下的按钮,python应用程序将关闭并返回一个特定值,该值将在另一个脚本中使用。我尝试了一些东西,但我不知道自己做错了什么。如果要运行它,请在脚本文件夹中放置一些名为warning.jpg的图片。

import wx
import gettext
import os
import time
global status

class MainFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id, "===WARNING!===",size=(1000,700), style =  wx.CAPTION )
        self.Centre()
        panel=wx.Panel(self)
        self.statusbar = self.CreateStatusBar(1)
        self.currentDirectory = os.getcwd()
        print(self.currentDirectory)
        warning = wx.StaticText (panel, -1, "Warning!!! Before you test, be sure you follow the instructions from the picture!", (150,5))
        font = wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        warning.SetFont(font)
        try:
            image_file = 'warning.jpg'
            self.image = wx.Image(image_file,wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            self.bitmap_image = wx.StaticBitmap(self, -1, self.image, pos=(10,30), size=(700,350))
        except:
             wx.MessageBox("Verify that in path:"+self.currentDirectory+"\n"+"you have a picture named: \"warning\" of JPG OR BMP type")
             quit()       
        self.DoneButton=wx.Button(panel,label='DONE!' ,pos=(150,500), size=(200,100))
        self.DoneButton.SetBackgroundColour('green')
        self.DoneButton.SetFont(wx.Font(15, wx.SWISS, wx.NORMAL, wx.BOLD))
        self.Bind(wx.EVT_BUTTON, self.Done, self.DoneButton)      
        self.CancelButton=wx.Button(panel,label='CANCEL!' ,pos=(500,500), size=(200,100))
        self.CancelButton.SetBackgroundColour('red')
        self.CancelButton.SetFont(wx.Font(15, wx.SWISS, wx.NORMAL, wx.BOLD))
        self.Bind(wx.EVT_BUTTON, self.Cancel, self.CancelButton)

    def Done(self, event):
        self.statusbar.PushStatusText('Done!!! Exiting...')
        print("Done! Exiting...")
        status = "ok"
        return status
        ##also to kill the program after the button is pressed 

    def Cancel(self, event):
        self.statusbar.PushStatusText('Exiting...')
        print("Cancel! Exiting...")
        status = "cancel"
        return status
        ##also to kill the program after the button is pressed 


if __name__ == "__main__":
    gettext.install("app")
    app = wx.App()
    wx.InitAllImageHandlers()
    frame_1 = MainFrame(None, wx.ID_ANY)
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:0)

关于我们的评论讨论和非常简单的术语:
通话程序:

from subprocess import call
print("executing external code")
return_code = call('python3 a1.py', shell=True)
if return_code == 5:
    print("Code executed correctly result: ",return_code)
elif return_code == 42:
    print("Code excuted correctly result: ", return_code)
else:
    print("Code failed")

return_code = call('python3 a1.py "parameter"', shell=True)
if return_code == 5:
    print("Code executed correctly result: ",return_code)
elif return_code == 42:
    print("Code excuted correctly result: ", return_code)
else:
    print("Code failed")

被叫程序(名为a1.py):

import sys
x=sys.argv
print("doing some work")
if len(x) > 1:
    sys.exit(42)
else:
    sys.exit(5)

结果:

executing external code
doing some work
Code executed correctly result:  5
doing some work
Code excuted correctly result:  42