Skip to content

widget.py - TypeError: can't convert float to int #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
caternuson opened this issue Dec 16, 2021 · 3 comments · Fixed by #67
Closed

widget.py - TypeError: can't convert float to int #66

caternuson opened this issue Dec 16, 2021 · 3 comments · Fixed by #67

Comments

@caternuson
Copy link

Re this thread:
https://forums.adafruit.com/viewtopic.php?f=47&t=186239

Added a dump of the values just before this line:

if (self._anchor_point is not None) and (self._anchored_position is not None):

thusly:

print(self._anchored_position[0], self._anchor_point[0], self._bounding_box[2], self._bounding_box[0])

and get:

6.0 0.0 93 0

So some of the values are float making the result also float. displayio.Group.x requires int.

@FoamyGuy
Copy link
Contributor

Interesting. I'm not sure why it was able to run successfully in the past.

anchored_position should generally be a tuple of ints since it represents a pixel location on the display. There are several places where the double divided by // operator is used convert the results of dividing to an int. Maybe there one or more spots where I missed that and it's dividing with /

No matter how it got the float into anchored_position though the Widget should handle it more gracefully. I think we can convert to int here before setting the value of x and y properties on the Group instance.

self.x = (
self._anchored_position[0]
- int(self._anchor_point[0] * self._bounding_box[2])
- self._bounding_box[0]
)
self.y = (
self._anchored_position[1]
- int(self._anchor_point[1] * self._bounding_box[3])
- self._bounding_box[1]
)

I can look into this more and make a PR for it tonight. There is a similar (but not exact same) issue resolved by one of the current PRs so I'll check that out tonight as well.

@caternuson
Copy link
Author

I did a quick test of simply adding a final int cast in the assignment:

self.x = int(

and same for self.y, and it fixed it.

But if this indicating an issue somewhere else, then might not be best fix.

@FoamyGuy
Copy link
Contributor

Off the top of my head I think it indicates a possible bug in the Touch Deck code. But if that does exist I'm not sure why it would have ever run in the past, so possibly there is nothing needing fixing in there after all.

I do think adding the int cast like you did is a good solution. Even if there is a bug elsewhere causing float values to get set to the anchored_position I think Widget should handle it more gracefully. Someone could do something like:

widget.anchor_point = (0.5, 0.5)
widget.anchored_position = (180.0, 140.0)

And I think we should not crash in that case ideally, which the int cast should allow us to avoid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants