I tried to use this script to prevent windows screen lock. The script works for moving the mouse, but it doesn't prevent windows 10 from locking.
import pyautogui
import time
import win32gui, win32con
import os
Minimize = win32gui.GetForegroundWindow()
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)
x = 1
while x == 1:
pyautogui.moveRel(1)
pyautogui.moveRel(-1)
time.sleep (300)
Yes it can. But sadly not by moving mouse which I don't know why and would like to know. So, my suggestion is to use pyautogui KEYBOARD EVENTS if possible. I have solved my problems by using VOLUME-UP & VOLUME-DOWN keys. Example code is provided below:
import pyautogui
import time
while True:
pyautogui.press('volumedown')
time.sleep(1)
pyautogui.press('volumeup')
time.sleep(5)
You can use any other keys if you want.
import ctypes
# prevent
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002)
# set back to normal
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000)
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
Tested on python 3.9.1, win 10 64bit
Related
Below code stops after printing hello and nothing happens.
Tried different option as pyautogui.leftClick(993, 578) but no luck.
In below code if I don't use subprocess but keep zoom GUI open then click is working fine.
import subprocess
import pyautogui
import time
import os
import pandas as pd
from datetime import datetime
print("hello")
subprocess.call("C:\\Users\xyz\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe")
time.sleep(5)
pyautogui.moveTo(993, 578, 0)
pyautogui.leftClick()
print(1)
The simple problem i see with your code is this line:
subprocess.call("C:\\Users\xyz\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe")
You've missed a \ before the xyz.
The following works perfectly for me on Python 3.6+:
import pyautogui
import time
import os
print("begun")
os.system(r"C:\Users\xyz\AppData\Roaming\Zoom\bin\Zoom.exe")
time.sleep(5)
pyautogui.moveTo(pyautogui.locateCenterOnScreen('my image'))
pyautogui.click()
print("dun")
If this doesn't work, try the pydirectinput module too, it's the updated version of pyautogui with much the same functions.
I want to minimize all open windows and / or show the desktop on a Windows 10 machine from within a python script. I had a look in the win32api, win32con, win32gui but cannot find anything appropriate.
Any idea appreciated. Thanks
Pyautogui is a great module for stimulating keyboard clicks and mouse clicks. To install it, try this command in the terminal. pip install PyAutoGUI
Using pyautogui, you can stimulate a virtual click in 2 ways. Choose which one works best for you:
1:
import pyautogui
pyautogui.hotkey('winleft', 'd')
2:
import pyautogui
pyautogui.keyDown('winleft')
pyautogui.press('d')
pyautogui.keyUp('winleft')
Sometimes the first one doesn't work, so if it doesn't, try the second one.
If you want to use WinApi to implement keyboard emulation, you can use the keybd_event function.
code:
import win32api
win32api.keybd_event(0x5B, 0, ) # LWIN
win32api.keybd_event(0x44, 0, ) # D
win32api.keybd_event(0x5B, 0, 2)
win32api.keybd_event(0x44, 0, 2)
Of course, you should probably use SendInput, but it is a bit complicated to use in python.
You can refer to this thread : How to generate keyboard events in Python?
I have a game written with a Pygame loop and it currently draws everything happening in the pygame window. I now want to integrate this window into a larger PySimpleGUI window to have nice functionality around the game. Is this possible?
I tried to use the code from here. The problem is that I get an error like this which comes from VIDEODRIVER at line 25:
pygame.error: windib not available
I changed this to 'windows' but then the Pygame window is separated from the PySimpleGUI as a different window.
Can I make the pygame loop as a window INSIDE the PySimpleGUI? Thank you.
It looks like the detached window is an open, unresolved issue with pygame 2.
If you're able to downgrade to pygame 1.9.6, the linked demo works as expected on Windows after changing the line 25 to:
os.environ['SDL_VIDEODRIVER'] = 'windows' as described.
As said there,
This line only work on windows:
os.environ['SDL_VIDEODRIVER'] = 'windib'
So make a code to skip it when the os is not Windows.
import platform
if platform.system == "Windows":
os.environ['SDL_VIDEODRIVER'] = 'windib'
I need to run a code for hours, and the computer I am working with has a (forced and unchangeable) screensaver policy. (It locks after 10 minutes). What can I do to prevent it? Is there any line of code to prevent that?
This worked for me. I'll just leave it here so people can use it.
import ctypes
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002) #this will prevent the screen saver or sleep.
## your code and operations
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000) #set the setting back to normal
You can prevent screen saver by moving mouse cursor on a fixed period of time, below function can handle cursor moving.
import win32api
import random
def wakeup():
win32api.SetCursorPos((random.choice(range(100)),random.choice(range(100))))
This is a coded solution that you can place in your program (also works for Mac users):
pip3 install pyautogui
https://pypi.org/project/PyAutoGUI/ (Reference)
import pyautogui
import time
def mouse_move():
while True:
pyautogui.moveTo(100, 100, duration = 1) # move the mouse
time.sleep(60) # Every 1 min
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()
Or, without the while loop, run it when required if your program is already within a while loop:
def mouse_move():
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()
I'm trying to set focus to a window using python-wnck.
The only docs I could find related to this library are from https://developer.gnome.org/libwnck/stable/WnckWindow.html
Using some code I found on another question here at SO, I was able to search for windows using the window title, but I'm not sure how to get a window to focus. From the above docs I found the function:
wnck_window_activate(WnckWindow *window, guint32 timestamp);
So in python I tried using this function like "window.activate(0)", but this appears to fail, the icon on my taskbar flashed but it doesn't get focus.In the terminal I get the message:
(windowTest.py:17485): Wnck-WARNING: Received a timestamp of 0; window activation may not function properly
So I think I may actually need to put in a valid timestamp but not sure how to get this.
This is the code Im using sofar:
import pygtk
pygtk.require('2.0')
import gtk
import wnck
import re
import sys
screen = wnck.screen_get_default()
while gtk.events_pending():
gtk.main_iteration()
titlePattern = re.compile('.*Geany.*')
windows = screen.get_windows()
for w in windows:
if titlePattern.match(w.get_name()):
print w.get_name()
w.activate(0)
The solution was actually pretty simpleI just needed to "import time" then pass "int(time.time())" into the activate function
Working code:
import pygtk
pygtk.require('2.0')
import gtk
import wnck
import re
import sys
import time
screen = wnck.screen_get_default()
while gtk.events_pending():
gtk.main_iteration()
titlePattern = re.compile('.*Geany.*')
windows = screen.get_windows()
for w in windows:
if titlePattern.match(w.get_name()):
print w.get_name()
w.activate(int(time.time()))
To get away from the Wnck-WARNING, you need to send a valid timestamp with the w.activate() function. The way that I found to do this is to use:
now = gtk.gdk.x11_get_server_time(gtk.gdk.get_default_root_window())
w.activate(now)
There really should be an easier way to do this, or wnck should allow a timestamp of 0 to mean now like most of the gtk libraries use.