【问题标题】:Writing to Python Shell from Visual Basic 6从 Visual Basic 6 写入 Python Shell
【发布时间】:2021-02-19 04:59:46
【问题描述】:

我有一个 python 模块“Cal.py”,如下所示:

def Plus(a, b):
    a1 = int(a)
    b1 = int(b)
    result = a1 + b1
    return result

def Minus(a, b):
    a1 = int(a)
    b1 = int(b)
    result = a1 - b1
    return result

在 Windows 10 中,我可以从 'cmd.exe' 以交互方式调用带有参数的函数:

D:\vb_python_SO>python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Cal
>>> Cal.Plus(2,3)
5
>>> Cal.Minus(5,3)
2
>>>

我想在 Visual Basic 6 中做同样的事情。 即VB6中的函数调用文本需要写在'>>>'之后,并且应该跟随'Return'键。 但我还没有找到任何好的解决方案。

我尝试了以下方法:

  • VB6 'Shell ("cmd /k python.exe")' 可以做上半部分,但在'>>>'之后不能继续。
  • 编写一个 *.bat(或 *.py),其中还包括 python 函数调用脚本。 通过Shell命令执行可以走得更远,但不能交互调用函数,其中参数不能从VB6文本框中一个接一个地更新。

附加信息)

我的目标是制作一个用于调用 Python 函数的 VB6 GUI 程序。我需要控制一个测量传感器某些特性的仪器。

更具体地说,

MyTester.py

def Init(_sensor_name='')
   #Sensor Initialization Process

def GetExposure()   
   #Read Present Exposure Time from Sensor Register

def SetExposure(Exposure_Time)
   #Write Exposure Time to Sensor Register

def FindExposure(...)

def MoveAbsolute(Rotation_Degee_A)
   #Rotate Table to Absolute Angle
 
def MoveRelative(Rotation_Degee_R)
   #Rotate Table by Relative Angle

def Roundtrip(...)

...

目前,函数调用在 Python Shell 中以交互方式执行。如果我制作一个VB6 GUI程序会变得更容易。请注意,应首先调用 Init() 函数,然后调用其他函数。

【问题讨论】:

  • 为什么需要一个程序来将输入放入交互式shell?我认为你需要展示你真正想要完成的事情。这样做看起来像是XY Problem
  • 感谢您的评论。我在帖子中进行了编辑以避免“XY 问题”。请参阅“附加信息”部分以了解我想要实现的目标。
  • 您可能想看看SendKeys 将击键发送到活动窗口的方法。也就是说,我确实认为这很尴尬,也许来自 Python 模块的 creating a DLL 并从 VB 调用它是一种更好的方法。免责声明:我不懂 Python,所以我不知道这有多难。
  • 我想也许您希望 VB6 调用 python somescript.py <params>。这样,您无需注入外壳即可获得所需的动态。您的参数可能类似于 operation_type [args]
  • 我感谢以上所有的 cmets。我认为“Sendkeys”方法是最适合我的情况的解决方案。制作 DLL 是一种优雅且语法正确的方法,但实现起来相当复杂。

标签: python shell cmd vb6


【解决方案1】:

我尝试了“Sendkeys”方法,发现它非常适合我的目的。添加了一些技巧来获得完整性。这是我的代码:

''''''''''''''''
'Form1.frm     '
''''''''''''''''

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
'Declare for using sleep function'

Dim RetVal As Integer

Private Sub Command3_Click()
'Prepare to call the functions in python module'

    RetVal = Shell("cmd", vbNormalFocus)    'Call DOS Cmd window'
    Sleep (1000)                            'Allow time for Shell Execution'
    Sendkeys ("d:")
    Sendkeys ("{ENTER}")
    Sendkeys ("python")                     'Call Python Shell'
    Sendkeys ("{ENTER}")
    Sleep (1000)                            'Allow time for Python Execution'
    Sendkeys ("import Cal")                 'Import the Python Module'
    Sendkeys ("{ENTER}")
    
End Sub

Private Sub Command4_Click()
'Execute to call each function in python module'

    AppActivate (RetVal)                    
    'Re-Obtain the App Focus. RetVal is the ProcessID of the running DOS Cmd Window.'

    Sendkeys ("Cal.Plus" + "+9" + Text1.text + "," + Text2.text + "+0")
    ' Arguments come from Text1.txt and Text2.txt'
    ' "(" --> "+9" and ")" --> "=0" according the Sendkeys convention.'
    Sendkeys ("{ENTER}")
    
End Sub

''''''''''''''''''
' Module1.bas    '
''''''''''''''''''

Public Sub Sendkeys(text As Variant, Optional wait As Boolean = False)
'Replacing Sendkeys Sub to keep from the Error "Permission Denied".'
'This code overrides the existing Sendkeys.'

   Dim WshShell As Object
   Set WshShell = CreateObject("wscript.shell")
   WshShell.Sendkeys CStr(text), wait
   Set WshShell = Nothing


以上只是简单代数 (Cal.py) 的一个示例,但我相信该方法适用于我的实际应用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多