简体   繁体   中英

Getting Python error: AttributeError: 'tuple' object has no attribute 'Text'

I will try to write as many details as I can.

I'm the author of a tool called Virtual Forms. It is an ActiveX server control.

For now, I have only used it from VBA, VB.NET & C# Now I want to use it from Python on windows. It works, but, I got stuck on one line of code.

Here is the code:

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"

So I can call methods and pass some parameters to this control.

The control accepts the VFFile, DatabaseType and the ConnectionString parameter, because it opens the Virtual Form, connect to the MySQL database and displays the data. Here is the screenshot:

在此处输入图片说明


. Then, in VBA I use this line of code to change the text that is in a textbox:

vf1.TextBox("VF2", "[customerid]").Text = "888"

And here is the screenshot that it works in VBA:


. 在此处输入图片说明

But when I try it from Python I got this error:

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

This only works in windows.

If you want to try it out you will need to install the setup for this Virtual Forms control that is in this download:

https://www.virtual-forms.com/sharing/Virtual%20Forms%20Framework2.0.0.31.zip

And here is the VFFile:

https://www.virtual-forms.com/sharing/VFFilePython.vf

Any suggestion is welcome.

Thanks,

Davor



UPDATE:

the print(dir(vf1)) gives this:

['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']

and the print(dir(vf1.TextBox("VF2", "[customerid]"))) gives this:

['__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']

Thank you all,

your directions moved me closer to the answer.

And the answer is, that I needed to include the [0] at the end.

So the error was in this line:

vf1.TextBox("VF2", "[customerid]").Text = "888"

And the answer is to add [0] to the end. Here is the code line that works:

vf1.TextBox("VF2", "[customerid]")[0].Text = "888"

Once again, thank you all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM