I am using pynput.mouse.Controller to listen for certain mouse actions and also using it to navigate to certain targets.
When I import Controller as follows: from pynput.mouse import Controller everything works fine and the programs runs smoothly.
However, when I do this instead import pynput.mouse.Controller I receive an error telling ModuleNotFoundError: No module named 'pynput.mouse.Controller'
Unless I am having a fundemental misunderstanding, these lines should function the same. Is there any reason why one produces an error but the other doesn't?
import imports modules or packages (directories with __init__.py), it cannot import objects from modules. This doesn't work:
import pynput.mouse.Controller
This work:
import pynput.mouse
Controller = pynput.mouse.Controller
This also work:
from pynput input mouse
Controller = mouse.Controller
And this:
from pynput.mouse import Controller
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.
This is my code.
from pynput.mouse import Button, Controller #Allows input to the game
from pynput.keyboard import Controller
mouse = Controller()
keyboard = Controller()
keyboard.press(Key.escape)
keyboard.release(Key.escape)
Why is "Key" not defined when all the sites I've looked at specifically say to use it without adding in anything else?
Sites:
https://pypi.org/project/pynput/
https://pynput.readthedocs.io/en/latest/
https://pythonhosted.org/pynput/keyboard.html
You have to import it first, as the example in the first link you provided showed:
from pynput.keyboard import Key
I am looking at a snippet of code and I just don't understand how it works:
import pygame, sys
from pygame.locals import *
on the first line pygame is imported, and on the second line, all the methods of a subset of pygame is invoked. If the first line imports all of pygame, why do we have to specifically import a subset of the module again? Why doesn't a mere import pygame do the job in the first place?
A mere import pygame would suffice, but the author wanted to have a shorthand access to the constants of pygame. For example, instead of:
import pygame
...
resolution = pygame.locals.TIMER_RESOLUTION
it may be sometimes preferable to have
import pygame
from pygame.locals import *
...
resolution = TIMER_RESOLUTION
Note that you should still import pygame itself to be able to access to other methods/properties (other than pygame.locals.) of pygame.
The idea is that you can call all the functions in pygame.locals without using pygame.locals.someFunction, but instead someFunction.
The following code runs fine within IDLE, but otherwise I get "NameError: global name 'messagebox' is not defined". However, if I explicitly state from tkinter import messagebox, it runs fine from where ever.
from tkinter import *
from tkinter import ttk
root = Tk()
mainFrame = ttk.Frame(root)
messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame)
Why does IDLE not need the explicit import statement but elsewhere it is required?
the messagebox is a separate submodule of tkinter, so simply doing a complete import from tkinter:
from tkinter import *
doesn't import messagebox
it has to be explicitly imported like so:
from tkinter import messagebox
in the same way that ttk has to be imported explicitly
the reason it works in idle is because idle imports messagebox for its own purposes, and because of the way idle works, its imports are accessible while working in idle
IDLE is written in Python and uses Tkinter for the GUI, so it looks like your program is using the import statements that IDLE itself is using. However, you should explicitly include the import statement for the messagebox if you want to execute your program outside the IDLE process.
messagebox.showinfo is defined inside tkinter/showinfo.py but when you use from tkinter import * you only import tkinter/__init__.py which holds the definitions of Label, Entry, Button, ... That is how python imports work.
When you use from tkinter import messagebox it looks for messagebox inside tkinter/__init__.py but it can't find it so it tries to import tkinter/messagebox.py
As for the IDLE anomaly, it is a bug in IDLE and I believe that it was patched.
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.