I have a problem with using DLL function in python.
I looked the name of function by dint of "dumpbin".
For example
_xGraphics3D#20
I tried to call the function like this:
from ctypes import *
xorsLib = cdll.LoadLibrary("Xors3D.dll")
width = c_int(800)
height = c_int(600)
depth = c_int(32)
mode = c_int(0)
vsync = c_int(0)
xGraphics3D = getattr(xorsLib,"_xGraphics3D#20")
xGraphics3D(width, height, depth, mode, vsync)
but it's cause the error:
Traceback (most recent call last):
File "D:/Coding/Python/pyASG/main", line 11, in <module>
xGraphics3D(width, height, depth, mode, vsync)
ValueError: Procedure called with not enough arguments (20 bytes missing) or wrong calling convention
what am i doing wrong?
p.s. i haven't know python, but i learn it. i read ctype-manual and tried to find answer...
p.p.s sorry for my awful english.
Try use windll instead of cdll.
xorsLib = windll.LoadLibrary("xors3d.dll")
http://docs.python.org/release/3.1.5/library/ctypes.html
The reason is the same as martineau commented.
Related
I am trying to use vtkImageStencil in python.
I am using the standard pip package "vtk 8.1.1"
import vtk
stencil = vtk.vtkImageStencil
image = vtk.vtkImageData()
stencil.SetInputData( image )
I am getting the following error:
Traceback (most recent call last):
File "<ipython-input-89-52c6c4badec2>", line 1, in <module>
stencil.SetInputData( image )
TypeError: no overloads of SetInputData() take 0 arguments
This does not make sense to me. Am I passing the wrong type?
Is there a workaround?
Stupid mistake, do not forget the parentheses when creating an object.
Change the example to:
stencil = vtk.vtkImageStencil()
This happens, when converting c++ code to python code.
I'm trying to use locateCenterOnScreen() function of PyAutoGUI, however it raises :
Traceback (most recent call last):
File "C:\Users\windows\Desktop\asd.py", line 3, in <module>
buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
TypeError: 'NoneType' object is not iterable
My code is:
import pyautogui
buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
pyautogui.doubleClick(buttonx,buttony)
How can I fix this problem?
From the documentation of Pyautogui here, the method locateCenterOnScreen returns None when it can't find the image on your screen.
Note that you are looking for 2 results from this method, but None is just one result (since the method normally returns two, this seems like bad design to me -- it should raise an exception instead, or at least return a tuple with two None objects).
Look at the following example, which is basically what is happening to you:
>>> foo,bar = None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
The simplest and most Pythonic way of addressing this in my opinion would be simply to try and catch it:
try:
buttonx,buttony = pyautogui.locateCenterOnScreen('who.jpg')
except TypeError:
""" Do something to handle the fact that the image was not found"""
EDIT:
To answer your question raised in the comments, there seems to be a misunderstanding with how this library works, or what it finds on the screen. You give the library a representation of what it needs to find via some image. It works far better when that image is lossless, because then it's an exact, pixel-by-pixel representation. The library then searches your computer screen for the actual thing represented by the image provided. It does not, as you raised concerns, find jpegs or pngs. It finds the actual rendered object. So, if you take a screen shot of the icon for your web browser on your desktop, it will find the actual icon from that screenshot and click on it, but only if it's visible. If it's behind other windows or something, it won't find it. It doesn't search the screen for the icon file, but the rendering of the icon itself. So, for instance, if you provided the actual .ico file to the library, it would not be able to find that icon if it was covered by another window, even though that icon is technically on your desktop, because it's not currently rendered.
To SIMPLIFY things - a locateCenterOnScreen() method should return the center (x,y) coordinates of an image as you called 'who.jpg' ONLY IF IT EXISTS ON THE SCREEN. Otherwise, if the 'who.jpg' image is not found on the screen - THE METHOD SHOULD RETURN 'NONE'.
You can not assign a 'NONE' value to (x,y) coordinates because you get a single value ('NONE') and two variables that are waiting to receive some value. This why you get a "TypeError".
Try to use an Exception, which is an event that may occur during the execution of a program -> for example a TypeError. Try to predict even other events that may occur during execution and you will be good to go with your task!
FOR YOUR CONVENIENCE TRY THE FOLLOWING CODE :
try:
buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
pyautogui.click(buttonx, buttony)
except TypeError:
print("A TypeError has been occured!")
if instead of x,y you just use one variable x,y would be on [0], and [1]
so it would be something like
location = pyautogui.locateCenterOnScreen(path)
then if not found it would return
location = None
else would be
location[0] = x and location[1] = y
If size of your image is 1920x1080, the image calls take about 1 or 2 seconds.
Therefore, try the code below.
import pyautogui
import time
time.sleep(2)
buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
pyautogui.doubleClick(buttonx,buttony)
You will be able to solve your difficulties.
I'm trying to call a COM method that requires a SafeArray of Strings to be passed as reference, which is then filled up with the method results. This is the code in VBA, which works flawlessly:
dimr RC as New RAS41.HECRASController
RC.Project_Open "c:\myProj.prj"
dim numMessages as Long
dim messages() as String
RC.Compute_CurrentPlan( numMessages, messages())
Now, I'm trying to do the same from with Python 3.4, using the win32com module. However, I'm stuck at trying to create the second parameter with the correct type, which according to combrowse.py should be "Pointer SafeArray String".
This was my first attempt:
import win32com
RC = win32com.client.Dispatch("RAS41.HECRASController")
RC.Project_Open("c:\\myProj.prj")
numMessages = 0
messages = []
RC.Compute_CurrentPlan(numMessages, messages)
I also tried constructing that variable as
messages = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_BSTR, [])
but it didn't work either. Error messages look like this:
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
print(o.Compute_CurrentPlan(1,b))
File "<COMObject RAS41.HECRASController>", line 3, in Compute_CurrentPlan
File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 282, in _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
TypeError: Objects for SAFEARRAYS must be sequences (of sequences), or a buffer object.
Make sure that you python variables are in the right format (Long and String). Try to use something like the following to get the variable types in shape:
messages = ['']
RC.Compute_CurrentPlan(long(numMessages), messages)
To be more flexible with your program you should check the variable types prior to the win32 call.
I realize this is an old question, but I ran into this issue and wanted to share the resolution. I was having issues defining the type of data for the first two arguments, but simply setting them to None works and your number of messages and compute messages are reported (I checked by assigning text = hec.Compute_CurrentPlan(None, None, True) and then print(test)). The third argument is Blocking Mode, set to True, meaning that the RAS computation will complete before moving to the next line of code. I am using Python 3.10.4 and HEC-RAS version 6.3.
import win32com.client
hec = win32com.client.Dispatch('RAS630.HECRASController')
hec.Project_Open(r"C:\myproj.prj")
hec.ShowRAS()
hec.Compute_CurrentPlan(None, None, True)
hec.QuitRAS()
I'm currently writing a python script which plots a numpy matrix containing some data (which I'm not having any difficulty computing). For complicated reasons having to do with how I'm creating that data, I have to go through terminal. I've done problems like this a million times in Spyder using imshow(). So, I thought I'd try to do the same in terminal. Here's my code:
from numpy import *
from matplotlib import *
def make_picture():
f = open("DATA2.txt")
arr = zeros((200, 200))
l = f.readlines()
for i in l:
j = i[:-1]
k = j.split(" ")
arr[int(k[0])][int(k[1])] = float(k[2])
f.close()
imshow(arr)
make_picture()
Suffice it to say, the array stuff works just fine. I've tested it, and it extracts the data perfectly well. So, I've got this 200 by 200 array of numbers floating around my RAM and I'd like to display it. When I run this code in Spyder, I get exactly what I expected. However, when I run this code in Terminal, I get an error message:
Traceback (most recent call last):
File "DATAmine.py", line 15, in <module>
make_picture()
File "DATAmine.py", line 13, in make_picture
imshow(arr)
NameError: global name 'imshow' is not defined
(My program's called DATAmine.py) What's the deal here? Is there something else I should be importing? I know I had to configure my Spyder paths, so I wonder if I don't have access to those paths or something. Any suggestions would be greatly appreciated. Thanks!
P.S. Perhaps I should mention I'm using Ubuntu. Don't know if that's relevant.
To make your life easier you can use
from pylab import *
This will import the full pylab package, which includes matplotlib and numpy.
Cheers
I currently am trying to use
paraview.simple.Histogram(Input, params)
as
paraview.simple.Histogram(q, BinCount = 30)
in the shell where q is a variable data set from my "out.e" ExodusII file. I'm getting the error
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'q' is not defined
I've tried to search the literature on python shell scripting in Paraview but it seems to be eluding me. I know this is a quick fix. Thanks
Try this instead:
Histogram(SelectInputArray="q", BinCount=30)
This assumes you currently have the reader as the active object in the Pipeline browser.
I was able to answer this problem using the following.
outset = Reader(FileName=['/dir/out.e'])
and for the Histogram
histogram1_1 = Histogram(Input=outset)
histogram1_1.SelectInputArray = ['CELLS', 'q']
histogram1_1.BinCount = 30
Note for anyone who comes into this issue, the TRACE option in the Python Shell will build a script for you when you do anything in the GUI.