展开全部
解题思路:把 Python 设计成带命令行参数调用的形式
在 VB6 中借助 IWshRuntimeLibrary.WshShell.Exec() 方法进行命令行参数调用
通过 IWshRuntimeLibrary.WshExec.StdOut.ReadAll() 方法读取命令行传e69da5e887aa62616964757a686964616f31333431346339回的结果
VB6 代码:Option Explicit
Private Sub Command1_Click()
' 请修改 CalcExp.py 为本地路径.
Text1.Text = ExecuteCommandLine("Python CalcExp.py -e """ & Text1.Text & """")
End Sub
' 执行命令行并返回结果.
Public Function ExecuteCommandLine(ByVal CommandLine As String) As String
Dim objWshShell As Object
On Error Resume Next
Set objWshShell = CreateObject("WScript.Shell")
If Not (objWshShell Is Nothing) Then
Dim objWshExec As Object
Set objWshExec = objWshShell.Exec(CommandLine)
ExecuteCommandLine = objWshExec.StdOut.ReadAll()
Set objWshExec = Nothing
Set objWshShell = Nothing
End If
End Function
Python 代码(CalcExp.py):import sys, getopt
from os import path
def calc(args):
return eval(args)
def GetCmdLine(args):
'''
从命令行读取输入的参数
'''
strCmdLne = ""
try:
lstOpts, lstArgs = getopt.getopt(args, "he:")
except getopt.GetoptError:
print("参数错误: 应为 -e")
sys.exit(2)
for strOpt, strArg in lstOpts:
if strOpt == "-h":
print("用法: %s -e <表达式>" % \
(path.basename(sys._getframe().f_code.co_filename)))
sys.exit()
elif strOpt == "-e":
strCmdLne = strArg
return strCmdLne
if __name__ == "__main__":
strExpression = GetCmdLine(sys.argv[1:])
if strExpression != "":
print(calc(strExpression))
动画演示:
PS:使用 IWshRuntimeLibrary.WshShell.Exec() 方法时会弹出命令行窗口!
扩展:Python 自带的 tkinter 库也可以创建界面的,建议你去看看这个库的使用!