I get the active window like so:
window = win32gui.GetForegroundWindow()
which is an Int, say 1053634.
And afterwards I try to set the foreground window back to the specified window:
win32gui.SetForegroundWindow(window)
And I get this error:
win32gui.SetForegroundWindow(window)
error: (127, 'SetForegroundWindow', 'The specified procedure could not be found.')
Sometimes when I do this in the interpreter, I get this error:
win32gui.SetForegroundWindow(1053634)
error: (0, 'SetForegroundWindow', 'No error message is available')
What do you think is the problem?
Thanks!
My program works fine on my desktop with Windows 7, but when I use my laptop with Windows Vista
(even with UAC off), I get the error:
pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')
The program will flash in the taskbar, but no characters are sent.
I even tried sending 'notepad' SetForegroundWindow and get the same error.
Here is a link with a workaround that combines threads to get the computer to think they work together: http://www.shloemi.com/2012/09/solved-setforegroundwindow-win32-api-not-always-works/
This article has more information about the problem.
UPDATE: I'm sorry that link goes to a C program. I researched some more and found out it will let you SetForegroundWindow, if you send an alt key first.
For example:
import win32gui, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(window)
This also worked for me
import win32gui, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(window.hwnd)
What worked it me...
import win32gui, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
input("Press Enter")
shell.SendKeys(' ') #Undocks my focus from Python IDLE
win32gui.SetForegroundWindow(window) #It works!
shell.SendKeys('%')
Related
I run the following commands and receive execution error.
Here is my code:
import webbrowser
test = "chrome://chrome-urls/"
webbrowser.open_new(test)
Error:
0:37: execution error: An error of type -10814 has occurred. (-10814)
What is my mistake? I use Pycharm on MacOs
Well, it seems there isn't much on this error, but what I could find is that -10814 is a kLSApplicationNotFoundErr. It probably means that the OS launch services can't associate the given URL with a default app to open it, which makes sense since you are trying to open an internal chrome URL. It probably won't throw the same error with standard http/https URLs.
This code works fine for me.
import webbrowser
test = 'https://stackoverflow.com/'
ff = webbrowser.get('google-chrome')
ff.open(test)
But this does not.
import webbrowser
test = 'chrome://chrome-urls/'
ff = webbrowser.get('google-chrome')
ff.open(test)
Maybe you cannot open this type of urls.
I was trying to automate opening multiple user profiles given a list of names on a few different sites but i can not find a way to open a link in a new window meaning i can not sort the different sites i am opening into their own window collection.
here is my code:
import webbrowser
chrome_path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
firefox_path="C:\\Program Files\\Mozilla Firefox\\Firefox.exe"
strURL = "http://www.python.org"
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)
webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(chrome_path),1)
webbrowser.open(strURL, new=0)
webbrowser.open(strURL, new=1)
webbrowser.open(strURL, new=2)
webbrowser.get('chrome').open(strURL)
webbrowser.get('firefox').open(strURL)
webbrowser.get('chrome').open_new(strURL)
webbrowser.get('firefox').open_new(strURL)
no matter what value i put for new (0, 1, or 2), all that ever happens is it opens a new tab in the last window i clicked on. i have tried all of the other methods that i found in they python documentation for the webbrowser module and everyone online is just saying to use "new=1" or webbroswer.open_new() but neither of those work. and even when i point it at firefox it just goes to chrome.
P.S.
i found a small workaround that i am not totally satisfied with.
import webbrowser
chrome_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
chrome_path_NW = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s --new-window"
firefox_path = "C:\\Program Files\\Mozilla Firefox\\Firefox.exe"
strURL = "http://www.python.org"
controller = webbrowser.get(chrome_path)
controllerNW = webbrowser.get(chrome_path_NW)
controllerNW.open(strURL, new=0)
controller.open(strURL, new=1)
controller.open(strURL, new=2)
controller.open("www.youtube.com", new=2)
the important thing to look at would be the "chrome_path" variable. i have changed it so it will run as a command and accept arguments. i found some launch arguments for chromium, here, that seem to work from chrome too. "--new-window" will open a new window and i can then open more tabs in that window but this is a total workaround of pythons module that i am not confident won't break if i am trying to use chrome while running this script. if there is any feature where i could group links together to open in specific windows that would be much more useful to me.
I realise this is a bit late but hopefully i can help someone in the future.
Basically you need to use the subprocess module to open up a new window before you load a new webpage
import subprocess
import time
import webbrowser
subprocess.Popen('open -a /Applications/Google\ Chrome.app --new', shell=True)
time.sleep(0.5) # this is to let the app open before you try to load a new page
webbrowser.open(url)
I am trying to automate a windows GUI machine.exe using pywinauto. I used the command app = Application().start("machine.exe") which throws this error after opening the machine.exe GUI
error: (0, 'WaitForInputIdle', 'No error message is available') in line 901 of application.py. I would like to understand the cause of the error message
I have tried to start the Gui using os.startfile and then connect to the gui using connect ()method
I am using python 2.6.6, OS is windows 7
from pywinauto.application import Application
def test():
app = Application().start("machine.exe")
app.connect(title="machine.exe")
I expected it to just open the gui without any issues.
just found the following . running via notebook
import
.echo("test")
Output:
/home/user/anaconda3/envs/p36/lib/python3.6/site-packages//utils.py in echo(message, file, nl, err, color)
257
258 if message:
--> 259 file.write(message)
260 file.flush()
261
UnsupportedOperation: not writable
Has someone seen this before and knows how to work around? I have to use a lib via that uses . so not is not possible.
Update:
This commit to a jupyter branch of click solves the issue:
https://github.com/elgalu/click/commit/1cb7aaba8c9dd6ec760d3e7e414d0b4e5f788543#diff-d17772ee4f65879b69a53dbc4b3d42bd
I think that Jupyter hijacks and locks the STDOUT/STDERR (at least the one click is trying to use) and if you don't provide a stream to click.echo() it will attempt writing to the STDOUT/STDERR, hence the error.
You can work around it by passing an output stream like STDOUT yourself:
import click
import sys
click.echo("test", sys.stdout)
# test
In my case using Python 3 I wanted to preserve the click styling on my message both in the Jupyter notebook and when the code was run in the terminal. I handled it this way:
from io import UnsupportedOperation
import click
item = 'Your Name'
message = click.style('"{}"'.format(item), fg='red', bold=True)
try:
click.echo(message)
except UnsupportedOperation as err:
print('Error: "{}"'.format(err))
print(message)
The color is preserved in the notebook:
I wrote the following python script to try to import a raster file into GRASS:
import os
os.chdir(r'C:\Users\Desktop')
import sys
sys.path.append(os.path.join(os.environ['GISBASE'], 'etc', 'python'))
import grass.script as g
import grass.script.setup as gsetup
gisbase = os.environ['GISBASE']
gisdb = 'C:\Users\Documents\grassdata'
location = 'newLocation'
mapset = 'mTW'
gsetup.init(gisbase, gisdb, location, mapset)
g.run_command('r.in.gdal', input = 'middle TW', output = 'middl_TW')
But I got this error:
ERROR: GISRC - variable not set
And then this box message showed up:
It means that the procedure entry point sqlite3_rtree_geometry_callback could not be located in the dynamic link library C:\OSGeo4W\bin\spatialite.dll
And then this showed up:
Seems that the r.in.gdal module crashed just as I called it.
If I directly use GRASS GUI, this box message showed up and the program crashed after I choose one mapset:
How to solve this problem? I am using GRASS 6.4.3, python 2.7.6 under WIN 8.1
After I uninstalled OSGEO4W by deleting its whole folder under C:\OSGEO4W, now I can launch GRASS and use its module normally.