GetHandle example?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

GetHandle example?

braidedlogix
I've been getting a """wxPyDeprecationWarning: Call to deprecated item. Use GetHandle instead. self.draw(window_dc.GetHDC(), x, y, width, height)""" warning for a drawing canvas on Windows. GetHandle appears not to be a drop-in replacement for GetHDC, so I was I was wondering if there was an example somewhere on the proper way to use this now.

Cheers, Eric

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GetHandle example?

Scott Talbert
On Thu, 27 Apr 2017, [hidden email] wrote:

> I've been getting a """wxPyDeprecationWarning: Call to deprecated item. Use
> GetHandle instead. self.draw(window_dc.GetHDC(), x, y, width, height)"""
> warning for a drawing canvas on Windows. GetHandle appears not to be a
> drop-in replacement for GetHDC, so I was I was wondering if there was an
> example somewhere on the proper way to use this now.

If I had to guess, I'd say you *probably* just need to convert it to an
int, try:
self.draw(int(window_dc.GetHandle()), x, y, width, height)

Scott

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GetHandle example?

braidedlogix
Sadly - no that doesn't do it. It complains about the number being too large and does not update the canvas.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GetHandle example?

Robin Dunn
In reply to this post by Scott Talbert
Scott Talbert wrote:

> On Thu, 27 Apr 2017, [hidden email] wrote:
>
>> I've been getting a """wxPyDeprecationWarning: Call to deprecated
>> item. Use
>> GetHandle instead. self.draw(window_dc.GetHDC(), x, y, width, height)"""
>> warning for a drawing canvas on Windows. GetHandle appears not to be a
>> drop-in replacement for GetHDC, so I was I was wondering if there was an
>> example somewhere on the proper way to use this now.
>
> If I had to guess, I'd say you *probably* just need to convert it to
> an int, try:
> self.draw(int(window_dc.GetHandle()), x, y, width, height)
>
> Scott

> Sadly - no that doesn't do it. It complains about the number being too
> large and does not update the canvas.

Here's probably what is happening.  GetHandle is returning a void* which
is returned to Python as a siplib.voidptr object. Since it is
representing a memory address then it will treat it as an unsigned value
when converting to an integer in its __int__() method. However if that
unsigned value is greater than the maxint value then it will return a
Python Long instead of an Int.  When you try to use that Long for
whatever it is that needs the HDC it is complaining because it wont
convert the Long back into a handle.

If you're not using a 64-bit build of Python and wxPython I would try
using that first.  Otherwise there is probably a way to convert the
value back into a C long with ctypes or something.


--
Robin Dunn
Software Craftsman
http://wxPython.org

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GetHandle example?

Robin Dunn
Robin Dunn wrote:
Scott Talbert wrote:
On Thu, 27 Apr 2017, [hidden email] wrote:

I've been getting a """wxPyDeprecationWarning: Call to deprecated
item. Use
GetHandle instead. self.draw(window_dc.GetHDC(), x, y, width, height)"""
warning for a drawing canvas on Windows. GetHandle appears not to be a
drop-in replacement for GetHDC, so I was I was wondering if there was an
example somewhere on the proper way to use this now.
If I had to guess, I'd say you *probably* just need to convert it to
an int, try:
self.draw(int(window_dc.GetHandle()), x, y, width, height)

Scott

Sadly - no that doesn't do it. It complains about the number being too
large and does not update the canvas.

Here's probably what is happening.  GetHandle is returning a void* which
is returned to Python as a siplib.voidptr object. Since it is
representing a memory address then it will treat it as an unsigned value
when converting to an integer in its __int__() method. However if that
unsigned value is greater than the maxint value then it will return a
Python Long instead of an Int.  When you try to use that Long for
whatever it is that needs the HDC it is complaining because it wont
convert the Long back into a handle.

If you're not using a 64-bit build of Python and wxPython I would try
using that first.  Otherwise there is probably a way to convert the
value back into a C long with ctypes or something.

I've started some changes for a future release (not the next one as that is already in the pipeline) that will make this a little easier. We've got mapped types for wxIntPtr and wxUIntPtr, which are integer types guaranteed to be large enough to hold a pointer value, which is what is needed for GetHandle. I'll switch the wrappers over to use that type instead and it should behave more like before but will be safe for cross platform and also different architectures.


--
Robin Dunn
Software Craftsman
http://wxPython.org

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GetHandle example?

braidedlogix

The following works to get rid of deprecation warning and accomplish the same thing as GetHDC():

window_dc = wx.PaintDC(window)
#old
#window_dc.GetHDC()
#new
import ctypes
ctypes.c_ulong(window_dc.GetHandle()).value

Thanks for pointing out the underlying type! I would never have thought to use ctypes here.

Probably the GetHandle should be adjusted to be drop-in replacement or the deprecation warning could be changed to point out the necessary conversion.

Cheers,Eric

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...