简体   繁体   中英

How can I use a value returned by a function in another application in python?

I want to know how can I return a value from an executable program developed in python, value that will be interpreted by another application. Also depending on what button is pressed the python application to shut down and return a specific value, which will be used in another script. I tried something, but I don't know what I'm doing wrong. Place some picture named warning.jpg in the script folder if you want to run it.

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

With regard to our comment discussion and in really simple terms:
Calling program:

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")

Called program (named a1.py):

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

Result:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM