[英]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.