简体   繁体   English

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

[英]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. 我想知道如何从以python开发的可执行程序中返回一个值,该值将由另一个应用程序解释。 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. 同样取决于按下哪个按钮,python应用程序将关闭并返回一个特定的值,该值将在另一个脚本中使用。 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. 如果要运行,请将一些名为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()

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): 调用程序(名为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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用返回的值在Python中打开类的另一个实例? - How can I use a returned value to open up another instance of a class in Python? 如何访问在另一个 function (Python) 中返回的 dataframe - How can I access a dataframe that is returned in another function (Python) 如何在其他函数中使用返回的变量。 Python - How can I use returned variable in other function. Python 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function? 如何在另一个 Python 文件中使用另一个 function 中的 function 的计算值? - How can I use a calculated value of a function in another function in another Python file? 如何将一个函数作为参数传递给另一个函数,并在python中使用该函数返回的值? - How to pass a function as argument to another function and use the value returned by that function in python? Python:我正在尝试使用另一个函数返回的函数 - Python: I am trying to use a function returned by another function python 将另一个 function 的返回值设置为在 header 中使用的变量 - python set returned value from another function as a variable to use in header 如何在另一个 function 中使用前一个 function 返回的变量? (Python) - How to use a returned variable from a previous function in another function? (python) 如何在另一个函数中使用从wxPython按钮返回的值? - How do I use a value returned from a wxPython button in another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM