How To Use Kivy's Core Image? - python

I have a grid layout and am adding to it some CoreImage widgets that I want to use for displaying pictures. I chose not to use uix.image since CoreImage is faster and I want to swap between a few pictures often.
When I do self.add_widget(CoreImage('Transparent.png')) my program will crash.
Traceback (most recent call last):
File "/path/to/my/app.py", line 1030, in <module>
MyApp().run()
File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 798, in run
root = self.build()
File "/path/to/my/app.py", line 702, in build
self.build_grid()
File "/path/to/my/app.py", line 696, in build_grid
self.grid.build_self()
File "/path/to/my/app.py", line 134, in build_self
self.add_widget(CoreImage('Transparent.png'))
File "/usr/lib/python2.7/dist-packages/kivy/uix/layout.py", line 80, in add_widget
size_hint=self._trigger_layout)
File "_event.pyx", line 436, in kivy._event.EventDispatcher.bind (kivy/_event.c:5429)
KeyError: 'size_hint'
What am I doing wrong? This works fine when I used uix.image.

I tried to do the same for showing a bunch of memory-loaded images. Something like this came out:
from kivy.uix.image import Image
from kivy.core.image import Image as CoreImage
self.add_widget(Image(texture=CoreImage('Transparent.png').texture))
Hope it helps.

kivy.core.image.Image (I assume this is what you mean) is not a widget, you can't use it like this. Use kivy.uix.image.Image.

Related

Auto clicker for games

Hello so I want to create a script in python that accepts my game instantly.
I do the following using IDLE SHELL:
import pyautogui
import os
**butt=pyautogui.locateCenterOnScreen('C:\\Users\\dariu\\Desktop\\autoclicker\\button.png', minSearchTime=2000)**
pyautogui.moveTo(butt)
pyautogui.click(butt)
pyautogui.click(butt)
After the bold line of code I got the following error:
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
butt=pyautogui.locateCenterOnScreen('C:\\Users\\dariu\\Desktop\\autoclicker\\button.png', minSearchTime=2000)
File "C:\Users\dariu\AppData\Local\Programs\Python\Python310\lib\site-packages\pyautogui\__init__.py", line 175, in wrapper
return wrappedFunction(*args, **kwargs)
File "C:\Users\dariu\AppData\Local\Programs\Python\Python310\lib\site-packages\pyautogui\__init__.py", line 207, in locateCenterOnScreen
return pyscreeze.locateCenterOnScreen(*args, **kwargs)
File "C:\Users\dariu\AppData\Local\Programs\Python\Python310\lib\site-packages\pyscreeze\__init__.py", line 413, in locateCenterOnScreen
coords = locateOnScreen(image, **kwargs)
File "C:\Users\dariu\AppData\Local\Programs\Python\Python310\lib\site-packages\pyscreeze\__init__.py", line 372, in locateOnScreen
screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.
File "C:\Users\dariu\AppData\Local\Programs\Python\Python310\lib\site-packages\pyscreeze\__init__.py", line 144, in wrapper
raise PyScreezeException('The Pillow package is required to use this function.')
pyscreeze.PyScreezeException: The Pillow package is required to use this function.
Looks like Pillow package is required in order to use one of your packages. Check the last line of the Traceback error
import this before the bold line: link to Pillow PyPI

In urwid 'Frame' object has no attribute 'rows'

I am searching any simple way to make urwid.Frame work in python without success, as example, I try this MWE:
frame = urwid.Frame(urwid.Text(('some text'), align='center'))
filler = urwid.Filler(frame, "top")
loop = urwid.MainLoop(filler)
loop.run()
And I get the following unuseful error message:
Traceback (most recent call last):
File "pycurses.py", line 596, in <module>
loop.run()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 287, in run
self._run()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 385, in _run
self.event_loop.run()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 790, in run
self._loop()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 818, in _loop
self._entering_idle()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 779, in _entering_idle
callback()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 574, in entering_idle
self.draw_screen()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 588, in draw_screen
canvas = self._topmost_widget.render(self.screen_size, focus=True)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/widget.py", line 145, in cached_render
canv = fn(self, size, focus=focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/decoration.py", line 814, in render
top, bottom = self.filler_values(size, focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/decoration.py", line 799, in filler_values
height = self._original_widget.rows((maxcol,),focus=focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/widget.py", line 205, in cached_rows
return fn(self, size, focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/container.py", line 1611, in rows
return sum(self.get_item_rows(size, focus))
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/container.py", line 1512, in get_item_rows
l.append(w.rows((maxcol,),
AttributeError: 'Frame' object has no attribute 'rows'
I try many variations, such putting the frame inside an urwid.Pile before giving it to the Filler widget, but I still get similar error messages.
So, is their any minimalist example work using urwid.Frame?
And what doese mean this “Frame' object has no attribute 'rows'”? I never ask in my example any .rows method, so why the error message have a behavior like if I used it?
The error is indeed not very good. The actual problem here is that the nesting of the Frame and the Filler should be the other way around:
import urwid
text = urwid.Text('some text', align='center')
filler = urwid.Filler(text, "top")
frame = urwid.Frame(filler)
loop = urwid.MainLoop(frame)
loop.run()
A flow widget (like Text) gets to decide how many rows it should be. The main loop expects a box widget (which does not get to pick its own width or height). The Filler widget bridges the two: it asks the wrapped flow widget how many rows it takes up (given a number of columns) and then fills up the rest of the rows.
The Frame is a box widget that contains another box widget in the center.
If you haven't seen this page already, I highly recommend looking at the diagrams here: http://urwid.org/manual/widgets.html#included-widgets The colors describe what kinds of widgets nest inside what kinds of widgets.
(Also I suspect it would be technically feasible for Urwid to detect that the widget is of the wrong type and say that instead in the error message, but I haven't dived that deep into the implementation.)

tkinter picture error: bad screen distance

I'm trying to write a tkinter program, and I came across an error that I've never seen before while working with tkinter. I've searched around everywhere, and tried everything I can think of. This is my code so far:
x=tk.Canvas(top,width=1000,height=750,bg="grey")
x.pack()
y=tk.PhotoImage(file="C:\\Users\\Admin\\Desktop\\images (3)_CnyokaDvJmG1xu.png")
x.create_image(top,0,0,image=y)`
and this is error
Traceback (most recent call last):
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
1705, in __call__
return self.func(*args)
File "C:/Users/Admin/.spyder-py3/temp.py", line 16, in open
x.create_image(top,0,0,image=y).pack()
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
2489, in create_image
return self._create('image', args, kw)
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
2480, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: bad screen distance "."
You don't have to supply containing widget when placing an image in a canvas, coordinates are enough:
x.create_image(0,0,image=y)
The error is because canvas does not accept a widget as coordinates.

Why did I get the error 'cannot load external tileset'

I just want to know how to fix the error that I mention in the title.
I really don't know what to try, there are only 3 lines of code and they are exactly how it's shown in the doc of pytmx...
import pygame
import pytmx
tmxdata = pytmx.TiledMap("map test pygame.tmx")
I just want to "load" the map that I create without this HUGE error:
Cannot load external tileset: C:\Users\WILHEM\Desktop\brdl Python 3\projet\labyrinthe\test pygame tileset.tsx
Traceback (most recent call last):
File "C:\Users\WILHEM\Desktop\brdl Python 3\projet\labyrinthe\testTMX.py", line 4, in <module>
tmxdata = pytmx.TiledMap("C:/Users/WILHEM/Desktop/brdl Python 3/projet/labyrinthe/carte test pygame.tmx")
File "C:\Users\WILHEM\Desktop\brdl Python 3\Python37-32\lib\site-packages\pytmx\pytmx.py", line 360, in __init__
self.parse_xml(ElementTree.parse(self.filename).getroot())
File "C:\Users\WILHEM\Desktop\brdl Python 3\Python37-32\lib\site-packages\pytmx\pytmx.py", line 400, in parse_xml
self.add_tileset(TiledTileset(self, subnode))
File "C:\Users\WILHEM\Desktop\brdl Python 3\Python37-32\lib\site-packages\pytmx\pytmx.py", line 845, in __init__
self.parse_xml(node)
File "C:\Users\WILHEM\Desktop\brdl Python 3\Python37-32\lib\site-packages\pytmx\pytmx.py", line 874, in parse_xml
raise Exception
Exception
If you can help me... Thank you very much from the bottom of my heart!
from pytmx.util_pygame import load_pygame
load_pygame(filename,pixilalpha=True)

Python Pillow - broken data stream

Pillow looks to be the solution to a lot of my problems, but I can't get it to work correctly. I think there might be something that I'm missing but I can't find any solutions that work.
from PIL import Image
img = Image.open("base64 (1).png")
print(img.size)
print(img.format)
img.show()
I have this set up to load an image, print its size and format, and then show the image. When I run the code, I get this:
(1920, 1080)
PNG
Traceback (most recent call last):
File "C:/Users/##########/PycharmProjects/weatherCanvas/imageTest.py", line 7, in <module>
img.show()
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1736, in show
_show(self, title=title, command=command)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2449, in _show
_showxv(image, **options)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2454, in _showxv
ImageShow.show(image, title, **options)
File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 51, in show
if viewer.show(image, title=title, **options):
File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 75, in show
image = image.convert(base)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 844, in convert
self.load()
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 250, in load
raise_ioerror(e)
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 59, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
The same thing happens if I replace img.show() with saving it. I know that PIL and Pillow save a temporary file in order to do the show() method, so I think the problem is somewhere in that.
Is there something I'm missing here?

Categories

Resources