Error while setting a bgpic with turtle - python

I'm new to the turtle module and I'm having a problem while setting a background pic for my turtle project.
when running this code:
import turtle as tr
import os
os.chdir(pathname)
tr.setup(400,400)
tr.bgpic("diamond.gif")
I get an error message for the 5th line ending with:
_tkinter.TclError: image "pyimage4" doesn't exist
Sometimes it's pyimage2 doesn't exist or pyimage36. At each execution it changes.
I didn't find a real solution in other posts. Any help will be much appreciated.

You're not showing us your actual minimal code that fails as your example doesn't get past this line:
os.chdir(pathname)
since pathname isn't defined. I downloaded this GIF, renamed it diamond.gif, and ran the following subset of your code:
import turtle as tr
tr.setup(400, 400)
tr.bgpic("diamond.gif")
tr.done()
This displays the GIF in a window:
If you repeat what I did, and it works, then this may be a problem with your GIF file. (Download the GIF from this link, don't use my PNG illustration above.) If you repeat what I did and it doesn't work, then it may be a problem with your environment. This error message:
_tkinter.TclError: image "pyimage4" doesn't exist
is often associated with independently initializing both the turtle and the tkinter modules. If you aren't doing such, perhaps you're running in a specially tweaked environment that is. There may be a workaround, but you first need to determine what's really happening.

Related

Why is pygame's display module not working on repl.it? [duplicate]

This question already has answers here:
is it possible to run pygame or pyglet in a browser?
(5 answers)
Closed 8 months ago.
I am making a Python program on Replit. My program uses pygame and thus must initialize it at some point to work. The module pygame.display is especially vital. However, whenever I call pygame.display.set_mode() or pygame.display.init(), I get this error:
Traceback (most recent call last):
File "main.py", line 11, in <module>
pygame.display.init()
pygame.error: No available video device
A similar error occurs when I call pygame.display.set_mode(). These functions are called while attempting to start the display, i.e.:
S_WIDTH = 500
S_HEIGHT = 500
screen = pygame.display.set_mode((S_WIDTH, S_HEIGHT))
This, of course, crashes the program.
The expected output is a screen 500 by 500 pixels wide. (Keep in mind that I have never had a chance to refine these numbers for a better display size, as the display has never worked in my program.)
I also tried importing NumPy in the same program, and it gave a very long message. Due to the correlation between these failures, the problem may be in Replit and not the libraries.
If you run the following code in Replit, you may get the same error:
import pygame
pygame.display.init()
That's the reproducible example. You can also call pygame.display.set_mode(500, 500) (with any two reasonable integers) in place of pygame.display.init(). You can also run my program to see the original error.
The problem with Repl is, it doesn't provide you a video card or something. So unlike your own computer, if you try to run a gui program on repl, it will not work - be it Tkinter or Pygame. Unless, you actually intialize a Repl with Pygame support.
Let's talk about what's wrong with the basic python repl.
So, what happens when you try to call pygame.display.init() or pygame.display.set_mode((w,h))? It first tries to figure out the display driver of the device it is running on. Since, repl is giving you just a bash console and online ide, it is not very realistic to expect having a video driver. Hence, you get the error.
However, this raises another question. Can't you run pygame on repl at all? Well, the doc says-
Does not require a GUI to use all functions. You can use pygame from a command line if you want to use it just to process images, get joystick input, or play sounds.
Which means, you can still run pygame and do stuff like, taking input etc. on repl. But nothing related to display because of the absence of video card.
But as you mentioned, The module pygame.display is especially vital., you have only several ways - Install pygame on your local computer or try getting a server that provides you with a GUI.
Or if you want to stick with ReplIt, you can start a Pygame Repl which is installed as a seperate language and isn't supported by the usual Python3 repl.
Edit: Thanks to #CharlesDuffy for updating me with the repl support for Pygame

pygame_sdl2 error: not implented

I'm building a android app using pygame_sdl2, pygame, and pgs4a, and when I tried to run my app on my PC (as a Python File, not an android app), I got this error:
You can find my code here.
Thanks for your time, OrionDark7
I figured it out, as Remolten said, some parts of pygame have not been implented into pygame_sdl2. But here is an alternative:
First, don't load pygame_sdl2, just pygame, and then the surface will blit to the window.
From there, you can take a screenshot of the surface you are trying to blit, and save it as an image.
Re-import pygame_sdl2, and load the image instead.
This is what I have tried and it worked, pygame_sdl2 can display images, but not some shapes.

Using PIL (Python Image Library) to detect image on screen

I am trying to understand how I can use PIL in Python 2.7 to search the whole screen for a certain image and click on it. I've been searching around and haven't been able to find a solution. I want to create a small GUI with one button in the middle of it that when clicked will search the entire screen for a predefined image. Once the image is found the program will then click in the centre of it and end. In short the program will detect if an image is present on the users screen and click it.
I did find an interesting bit on Sikuli, but that doesn't help me because it's unable to export to an .exe.
The image that the program will look for will most likely be in the same place each time it searches, but I didn't want to hard-code the location as it has the potential to move and I don't want that being an issue later on.
What I need is the code method I would use to search for the image on screen and send back the cords to a variable.
Image explanation/example:
Reference image of rifle:
PIL is the wrong tool for this job. Instead you should look into openCV (open source computer vision), which has fantastic python bindings. Here is a link to an example (in C but should be easy to redo with the python bindings) that does what you are looking for, but even allows the image to be rotated, scaled, etc.
http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
http://docs.opencv.org/doc/tutorials/features2d/detection_of_planar_objects/detection_of_planar_objects.html
Edit:
I assume you are using windows, as your example image looks like window. In this case you can use:
from PIL import ImageGrab
pil_img = ImageGrab.grab()
opencv_img = numpy.array(pil_img)
then use opencv to process the image to find sub image you are looking for.
If you want to do this cross platform, then you will need to use wxWidgets to do the screengrab: https://stackoverflow.com/a/10089645/455532
Even I wanted to do the same but using different module - pyautogui. I finally found the solution for my problem and I am sure this solution will also help you.
You have to just go to this webpage and read the locate function topic completely
and you'll be able to solve your problem.
I recommend you give a look on PyAutoGUI, a well documented library to control mouse and keyboard, also can locate imagens on screen, find the position, move the mouse to any location and clicks on location, also can simulate drag and drop, type on input fields, give double clicks and much more.

How to capture the screen output of a running program?

Is it possible to take screenshots of a running program (with GUI) from another python program ?
If so, what could be the steps and libraries that I could use ? (On Windows)
For example, let's say I have calc.exe running. I'd want to take screenshots of what is displayed to the user from myprogram.py.
My goal is to analyze what's displayed on the monitored program.
If it's not possible to isolate the screenshot to a running predefined program, I think I will have to take screenshots of the fullscreen but it's not very practical.
Capturing an screenshot is easy. Just install the Python Imaging Library and use the ImageGrab.grab() function to return an Image instance with the screenshot.
Capturing an specified window is a little more complicated, because you need the window coordinates. I recommend you to install the win32api modules and use a little module called winGuiAuto.py. Once you do that, you can do something like this:
hwnd = winGuiAuto.findTopWindow(title)
rect = win32gui.GetWindowPlacement(hwnd)[-1]
image = ImageGrab.grab(rect)
However, capturing the screen is the easy part. If you want to analyze the contents from screenshots, you're in for a lot of complications. This is probably the wrong approach for doing what you want and should be left as a last resort.
In most cases, it's easier to use the windows api to read the contents of a window's elements directly, but that won't work with some 3rd party GUI toolkits. That's not within the scope of your question so I'm not detailing it here, but you should read the source of the winGuiAuto.py module mentioned above for examples on how to do that, as well as checking the pywinauto library.
The ImageGrab Module, works on Windows only. The pyscreenshot module, is a better replacement for that, can be used to copy the contents of the screen to a PIL or Pillow image memory. Read more at link below.
https://pypi.python.org/pypi/pyscreenshot

pyscripter auto-suggestions with pygame

I'm not sure if this question is appropriate for Programmers, but I thought it was a better fit here than on SO. I have these significant lines:
#!/usr/bin/env
import sys
import pygame
from pygame.locals import *
Specifically in PyScripter, I cannot see most functions of pygame. For example, I cannot see anything in locals. If I type pygame. , then a list pops up with
Color
MissingModule
os
packager_imports
string
sys
warnings
warn_unwanted_files
color_constructor
color_reduce
rect_constructor
rect_reduce
__version__
However, I believe that there are missing elements, at least according to the pygame reference. Such elements would include (I think) all of the modules loaded by pygame, so it is a big deal that I can get these auto-suggestions to work. What might I need to change in order to be able to access this from Pyscripter? (If this is difficult, would there be a simple workaround?)
I'm Japanese and I'm just beginning to use python and pygame.
When I bought a new PC, I downloaded Pyscripter, python 3.3, and pygame.
As you said, Pyscripter couldn't work with the pygame namespace.
Pyscripter emitted only the above code in the list.
The only function of pygame that displayed was Color.
I didn't know what to do with pygame in Pyscripter.
But, I tried to write a python interpreter attached to Pyscripter.
And I wrote as follows:
import pygame
from pygame.locals import *
pygame.init()
SCREEN = pygame.display.set_mode((640,480))
pygame.quit()
At that time, I still hoped that it would work. Pyscripter could work with pygame.
I wrote the codes and I finished pygamewindow at the same time.
Then, I returned to the original browser (whatever it's called).
It was either repaired or recognized.
I was very bothered at first, but when I think about the later invention, that is not suffered.
However, there is a downside to this solution. If I write a code, for example;
pygame.surface.Surface
Auto-completion, in the middle of the above code, didn't emerge.
I have no idea why it didn't, but now I write all of my code with Pyscripter.
It is a little inconvenient, but it made is easier to program. If you solved this problem and understand the reason for it, please let me know. I hope that this will help you and others. Bye.

Categories