Module won't work in IDLE - python

This has been troubling me for the past hour. My python script won't run; however, if I select 'check module', input the code, and run the same thing it works. It's a very simple script:
import pyscreenshot as getImage
im = getImage.grab(bbox=(1300,800,1500,850))
im.save("screen.png")

Try running through terminal and it will work.
Although I do not know why this happens exactly (probably ask those who maintain it) I can tell you it is because of some terminal dependant function that is created while running.
The IDLE is not actually a terminal so it cannot run exactly like a terminal (although it outputs the same content, it is not a terminal). For example running os.get_terminal_size() under the IDLE will not work yet the terminal will. There are also some functions in PIL that perform in the same way.
Anyway this post shows a pretty similar code and it is mentioned it doesn't work under the IDLE.

You need to set childprocess=False as IDLE has problems with multiprocessing. IDLE runs user code in a separate process so with pyscreenshot IDLE hangs and seems like the module won't work. You may go through this to know more.
An option is to turn off multiprocessing by setting childprocess=False.
Try:
import pyscreenshot as getImage
im = getImage.grab(bbox=(1300,800,1500,850), childprocess=False)
im.save("screen.png")

Related

Screenshot with python pyautogui on windows - how to prevent opening shell or cmd

I want to take a screenshot with python using pyautogui. This is my code:
import pyautogui
im1 = pyautogui.screenshot("my_screen.png")
My problem is that this way half of my screen is blocked by the python shell, which pops up when I start the code. So the picture doesn´t really is a screenshot. Prior I have used python.exe to run python files and then the problem was the cmd window poping up. Now I use pythonw.exe, because apparently this prevents the cmd window to show up, but now the python shell window blocks my screen.
So does anyone know how I can take a clean screenshot?
Well, the solution was quite simple. After trying to take a screenshot with pillow I got the same result. So the answear is in the configuration of Spyder, which I was using. Under configuration and "run" you can change where the code will run. And I had chosen that the code shall ran in a external window. So I changed to internal and everything works as it should.
make it sleep for 5 seconds maybe? enough to minimize what is not necessary to capture
import pyautogui
pyautogui.sleep(5)
im1 = pyautogui.screenshot("my_screen.png")

get active window in python

I'm bored and just making something for the heck of it in Python. I saw someone typing with spaces between all their letters and decided to make a python script that does this. It was pretty easy, but then I wanted to take it a step further, because copy/pasting from console takes time, so I want to have this script put spaces after every keyboard press but only when I have Discord as the active window. The only things I could find that could give you the active window are from 5-15 years ago, and are all outdated. They say use win32gui, and I pipinstalled it, but it doesn't seem to work.
EDIT: For clarification, I ran "pip install win32gui" and it installed, I opened a python shell and typed "import win32gui" and it said no such module
I looked through the modules and found win32 and according to the help command win32gui is part of its package, so I tried win32.win32gui and it says there's no such attribute
I'm new to coding, I'm not entirely sure what I'm doing.
It seems win32gui isn't recognized in shell which I was using to test if it worked or not, however after just adding it to an actual python script it works just fine.

How to stop console from poping up when command is called from python GUI?

I have made a GUI for my application. All scripts are in Python (2.7) and the GUI is created with Tkinter. I work on Linux, but I needed this app to be executable on Windows. So I've used py2exe, to create an executable. After a while it is working almost perfectly, but I have the following problem:
Somewhere in the application, I need to call external programs, namely ImageMagick and LaTeX. I use the commands convert, pdflatex, simply by importing os module and running os.system(build), where build = 'convert page.pdf page.gif'etc. When those commands are called from the *.exe application the console pops up (meaning a console window opens up for a split of a second and closes again). Is there a way, to prevent this behaviour?
It does not interrupt the application, but it is ugly and not a desired behaviour.
[Note] I chose not to add any samples, since there are lots of files and other content, which, I think, is not related to the problem. I could however try to post minimal (not)working example. But maybe it is not needed.
Thanks!
import subprocess
subprocess.Popen("application.exe", shell = True)

I'm new to Python and managed to crash IDLE after adding a line of code to my program

I'm working on a small game and, in the interest of full disclosure, I've learned some other languages before but this is only my second day learning Python.
What I was trying to do was simple enough: I was trying to generate a random integer between two integer values (e.g. random(a,b)). I looked around to see if there was an existing function that I could use, and I found information about a function called "randint". So I added a line of code to my program that looked something like:
value = randint(1,15)
I received an error that randint was undefined. So then I looked further and saw someone state that I needed to import the random library using the following line of code:
import random
The moment I refreshed (F5) IDLE crashed. So then I figured "welp, that was wrong" and I went to open IDLE and make the change. I was able to open IDLE, but I can't open the file. In fact, I can't open any of the (three) files that I've made. I opened the file in Notepad++ and removed the offending lines (both randint and import random), but IDLE still crashes whenever I try to open a file. I rebooted my laptop for lack of a better idea, but there was no change in behavior.
Details: Windows 7 x64 w/ all updates, Python 3.2.3.
...help? Also, what did I do? :-/
Received a solution from another forum:
"If you want to try diagnosing the problem, run the Python command line and then enter the line:
from idlelib import idle
That will launch idle, and you should get a traceback in the command prompt if something goes wrong."
When I did this it opened IDLE and produced an error (displayed in terminal) when I tried to open the file. Rather than crashing, I received a prompt (pop-up, not in terminal) to save the file. I found this odd because I had just opened it and hadn't made any changes. I let it save the file and then success! Now I can use IDLE to open any of my files again. Hope this helps someone else who encounters this issue :)
I'm not sure if this is helpful, but based on my experience, IDLE was never reliable enough for me. Practice using the command-line Python instead.
Go to the command prompt by running cmd
cd into your file's directory
type python yourfilename.py

Emulating a Windows shortcut in Python

This is probably a fairly simple question, but I have gone thoroughly overwhelmed trying to figure it out.
I want to start another program in Windows from Python 2.6. I have the "command-line argument" figured out so that if I create shortcut and double-click on it the other program opens, does what it needs to, and then closes.
I started with the subprocess library, but that seemed not to work. I got overwhelmed looking at all of the different versions of "popen"
How do I run in an external program from Python like I had double-clicked on a shortcut?
Something like below maybe?
import os
os.system("notepad")
Or:
from subprocess import call
call(["notepad"])
The below question has some excellent answers:
Calling an external command in Python

Categories