我会尽力写出尽可能多的细节。
我是一个叫做Virtual Forms的工具的作者。这是一个ActiveX服务器控件。
目前,我仅在VBA,VB.NET和C#中使用过它 现在,我想在Windows上的Python中使用它。 它可以工作,但是我只能停留在一行代码上。
代码如下:
import win32com.client as win32
vf1 = win32.gencache.EnsureDispatch('VirtualForm2.VirtualForm')
vf1.VFFile = r"C:\Users\WinPIS\Desktop\VFPython\VFFilePython.vf"
vf1.DatabaseType = 2
vf1.ConnectionString = r"DRIVER={MySQL ODBC 5.3 Unicode Driver};" \
"Port=3306;" \
"SERVER=myserver;" \
"DATABASE=mydatabase;" \
"USER=imtheuser;" \
"PASSWORD=hereismypass;" \
"OPTION=3;"
#vf1.OpenVirtualFormDesigner()
vf1.ShowVirtualForm("VF2")
vf1.TextBox("VF2", "[customerid]").Text = "888"
所以我可以调用方法并将一些参数传递给此控件。
该控件接受VFFile,DatabaseType和ConnectionString参数,因为它可以打开虚拟窗体,连接到MySQL数据库并显示数据。这是屏幕截图:
。 然后,在VBA中,我使用以下代码行来更改文本框中的文本:
vf1.TextBox("VF2", "[customerid]").Text = "888"
这是在VBA中工作的屏幕截图:
但是当我从Python尝试时,出现此错误:
C:\Users\WinPIS\venv\VFPython\Scripts\python.exe
C:/Users/WinPIS/PycharmProjects/VFPython/VFPythonFile.py
Traceback (most recent call last):
File "C:/Users/WinPIS/PycharmProjects/VFPython/VFPythonFile.py", line 20, in <module>
vf1.TextBox("VF2", "[customerid]").Text = "888"
AttributeError: 'tuple' object has no attribute 'Text'
Process finished with exit code 1
这仅适用于Windows。
如果您想尝试一下,则需要安装此下载中的此Virtual Forms控件的安装程序:
https://www.virtual-forms.com/sharing/Virtual%20Forms%20Framework2.0.0.31.zip
这是VFFile:
https://www.virtual-forms.com/sharing/VFFilePython.vf
欢迎任何建议。
谢谢
Davor
更新:
print(dir(vf1))
给出了这个:
['CLSID', 'CloseAllVirtualForms', 'CommandButton', 'ConnectDatabase', 'DisconnectDatabase', 'EditRefresh', 'EditState', 'EditStateChange', 'GridRefresh', 'OpenVirtualFormDesigner', 'SectorVisible', 'ShowAboutBox', 'ShowVirtualForm', 'SplashShow', 'Splashhide', 'TextBox', 'TriggerBeforeFormOpen', 'TriggerCommandButtonAction', 'TriggerCommandButtonClick', 'TriggerCommandButtonGroupChange', 'TriggerCommandButtonViewClick', 'TriggerEditAfterSave', 'TriggerEditBeforeSave', 'TriggerEditChange', 'TriggerFlexChange', 'TriggerGenerateID', 'TriggerMenuFunction', 'TriggerTextBoxChange', 'TriggerTextBoxCheck', 'TriggerTextBoxGotFocus', 'TriggerTextBoxInit', 'TriggerTextBoxLostFocus', 'TriggerTextBoxValidate', 'VFInternalCheck', 'VirtualForm', '_ApplyTypes_', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_good_object_', '_get_good_single_object_', '_oleobj_', '_prop_map_get_', '_prop_map_put_', 'coclass_clsid']
和
print(dir(vf1.TextBox("VF2", "[customerid]")))
给出了这个:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
答案 0 :(得分:0)
谢谢大家
您的指示使我离答案更近了。
答案是,我需要在最后加上[0]。
所以错误在这一行:
vf1.TextBox("VF2", "[customerid]").Text = "888"
答案是在末尾添加[0]。这是有效的代码行:
vf1.TextBox("VF2", "[customerid]")[0].Text = "888"
再次感谢大家