首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Visual Basic 6写入Python Shell

从Visual Basic 6写入Python Shell
EN

Stack Overflow用户
提问于 2021-02-19 04:59:46
回答 1查看 239关注 0票数 0

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

代码语言:javascript
复制
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‘中的参数交互调用函数:

代码语言:javascript
复制
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 'Shell ("cmd /k python.exe")‘可以完成上半部分,但不能在can >’>之后继续.
  • 编写*.bat (或*.py),其中也包括python函数调用脚本.由Shell命令执行该命令可以更进一步,但函数不能交互调用,其中参数不能从VB6 textbox.

逐个更新。

其他资料)

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

更确切地说,

MyTester.py

代码语言:javascript
复制
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()函数,然后调用其他函数。

EN

回答 1

Stack Overflow用户

发布于 2021-02-21 05:38:02

我尝试过“Sendkey”方法,发现它对我的目的很好。为了获得完整性,我们增加了一些技巧。这是我的密码:

代码语言:javascript
复制
''''''''''''''''
'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)的一个例子,但我相信这个方法将适用于我的实际应用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66272017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档