我有一个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 6中做同样的事情,也就是说,从VB6调用函数的文本需要写在'>>>‘之后,返回键应该遵循。但我还没有找到好的解决办法。
我试过以下几种方法:
逐个更新。
其他资料)
我的目标是制作一个调用VB6函数的Python程序。我需要控制一个测量传感器某些特性的仪器。
更确切地说,
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中交互执行的。如果我制作一个VB6图形用户界面程序,它会变得更容易。注意,首先应该调用Init()函数,然后调用其他函数。
发布于 2021-02-21 05:38:02
我尝试过“Sendkey”方法,发现它对我的目的很好。为了获得完整性,我们增加了一些技巧。这是我的密码:
''''''''''''''''
'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)的一个例子,但我相信这个方法将适用于我的实际应用。
https://stackoverflow.com/questions/66272017
复制相似问题