如何在python的另一个应用程序中使用函数返回的值?
我想知道如何从用python开发的可执行程序返回一个值,该值将由另一个应用程序解释。还取决于按下哪个按钮,python应用程序将关闭并返回一个特定值,该值将在另一个脚本中使用。我试过了,但我不知道我做错了什么。如果要运行,请在脚本文件夹中放置一些名为如何在python的另一个应用程序中使用函数返回的值?,python,python-3.x,return,wxpython,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
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()
关于我们的评论讨论,简单地说:
呼叫程序:
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):
结果:
executing external code
doing some work
Code executed correctly result: 5
doing some work
Code excuted correctly result: 42
如果您没有时间限制,只需将第一个脚本的输出写入一个文件,然后在第二个脚本中读取它,这对我来说不是一个选项,它需要尽可能快关于
os.pipe()。一次是否运行多个脚本?脚本终止后结果是否可用?如果有多个脚本,它们是否必须同时运行?是否存在服务器、客户端关系。没有细节,有太多的选择,无法提供一个像样的答案。管道、插座、文件etc@RolfofSaxony基本上,我希望用python开发的可执行文件能够在按下按钮时返回值。这个可执行文件将用于另一个程序(spectrum软件),它可以选择加载外部程序(cvi、dll、exe等)并读取返回值。它调用的是我的python可执行文件,所以它们必须同时运行
executing external code
doing some work
Code executed correctly result: 5
doing some work
Code excuted correctly result: 42