I just started learning python and I have made this script that types for me, but I couldn't figure out how to make it press the enter key.
import time
keyboard = Controller()
time.sleep(3)
for char in "Hello, this isn't notking but this is a bot, he has been learning python and learned this script that types for him!":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.12)```
This should help you with your problem. Next time please provide the whole code related to the question.
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
time.sleep(3)
for char in "Hello, this isn't notking but this is a bot, he has been learning python and learned this script that types for him!":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.12)
keyboard.press(Key.enter)
keyboard.release(Key.enter)
I am trying to use this script with my raspi to open another script when a button is released, and close the opened script when the button is pressed.
If i hold the button i get a constant feed of "camera is not on" which is good thats what i want it to do, Then when i release the button the script opens as it should and my output says "camera is on" and the camera turns on also good. I need to call this script to open the camera.
This is what I have so far.
from gpiozero import Button
import sys
import os
import subprocess
button = Button(17)
p = subprocess.Popen(['python', 'objectident.py', 'arg1', 'arg2'])
while True:
if button.is_pressed:
print("camera is not on")
p.terminate()
p.wait()
else:
print("camera is on")
os.system("python objectident.py")
continue
This is the output that I am getting.
Image of output
any help would be greatly appreciated this is my first big python project, and for some reason i cant find the solution i am looking for.
Hey I'm a beginner programmer, trying to write some code to press the letter 'k' on the keyboard every 4 seconds, while also being able to shut down the program with a keystroke WHILE IN A DIFFERENT WINDOW.
I've tried using this,
import time
import pyautogui
def kicker():
while True:
time.sleep(4)
pyautogui.press('k')
try:
while True:
kicker()
except KeyboardInterrupt:
pass
but I can only KeyboardInterrupt while the window I am coding in (jupyter notebook) is open, when I go into another window (with jupyter still open in the background) I can no longer interrupt the loop.
How can I make it so I can interrupt the loop on a keypress (not necessarily KeyboardInterrupt) while not on the jupyter notebook window?
You may want to use pyxhook to listen for all keystrokes on your machine. Do note that this is roughly the same as a keylogger and might involve admin access on your machine so there may be some safety concerns to think about.
Here is an example script from the repo that showcases it printing the event, but the line of interest is the function on line 15:
def kbevent(event):
global running
if event.Ascii == <...Put ascii code for k here...>:
running = False
I started to learn pyautogui for my personal project and almost instantly ran into the problems when trying to open OSX dock icons.
I want to open local Spotify which is under Mac Launchpad.
My code to do this.
import pyautogui
launchpad = pyautogui.locateOnScreen('img/Launchpad.png')
This return None so the image was not found.
image example attached
However, if I open Mac OSX Notes window and paste the same image into it and ran the program again the image is found every time. Similarly, if I just leave image open in my Editor.
Is dock actually part of the OSX screen pyautogui can search from? If not how to interact with it?
Figured that using application hotkeys vs find on the screen is a much less brittle approach. Below how I finally build Spotify bot.
import time
import pyautogui
# use pyauutogui key shortcut to open OSX spotlight search
pyautogui.hotkey('command', 'space')
# type spotify and press enter to open application
pyautogui.typewrite('Spotify')
pyautogui.hotkey('enter')
# use Spotify keyboard shortcuts to select search.
# key docs here: https://support.spotify.com/ie/article/Keyboard-shortcuts/
time.sleep(5)
pyautogui.hotkey('command', 'l')
# typewrite allows passing string arguments using keyboard
pyautogui.typewrite('concentration music')
# move to select the song with tab and press enter to play
pyautogui.hotkey('tab', 'tab', 'tab', 'tab')
time.sleep(2)
pyautogui.hotkey('enter')
pyautogui.hotkey('space')
# sleeps 30 seconds while music is playing
time.sleep(30)
pyautogui.hotkey('command', 'q')
I made a program to pertaining your problem. It is a spotify bot aswell.
import pyautogui as p
from time import sleep as t
p.keyDown("command")
p.press("space")
t(2)
p.keyUp("command")
p.typewrite("Spotify")
p.press("enter")
t(3)
p.moveTo(153,132)
p.click()
t(1)
p.typewrite("Never Gonna Give You Up")
p.press("enter")
p.moveTo(707,324)
t(4)
p.click()
t(10)
p.keyDown("command")
p.press("q")
t(1)
p.keyUp("command")
The coordinates will be different pertaining to the position of your spotify window, so you can do p.mouseInfo/pyautogui.mouseInfo to find the exact coordinates and substitute them in the code I gave above.
Is it possible to produce an alert similar to JavaScript's alert("message") in python, with an application running as a daemon.
This will be run in Windows, Most likely XP but 2000 and Vista are also very real possibilities.
Update:
This is intended to run in the background and alert the user when certain conditions are met, I figure that the easiest way to alert the user would be to produce a pop-up, as it needs to be handled immediately, and other options such as just logging, or sending an email are not efficient enough.
what about this:
import win32api
win32api.MessageBox(0, 'hello', 'title')
Additionally:
win32api.MessageBox(0, 'hello', 'title', 0x00001000)
will make the box appear on top of other windows, for urgent messages. See MessageBox function for other options.
For those of us looking for a purely Python option that doesn't interface with Windows and is platform independent, I went for the option listed on the following website:
https://pythonspot.com/tk-message-box/ (archived link: https://archive.ph/JNuvx)
# Python 3.x code
# Imports
import tkinter
from tkinter import messagebox
# This code is to hide the main tkinter window
root = tkinter.Tk()
root.withdraw()
# Message Box
messagebox.showinfo("Title", "Message")
You can choose to show various types of messagebox options for different scenarios:
showinfo()
showwarning()
showerror ()
askquestion()
askokcancel()
askyesno ()
askretrycancel ()
edited code per my comment below
You can use PyAutoGui to make alert boxes
First install pyautogui with pip:
pip install pyautogui
Then type this in python:
import pyautogui as pag
pag.alert(text="Hello World", title="The Hello World Box")
Here are more message boxes, stolen from Javascript:
confirm()
With Ok and Cancel Button
prompt()
With Text Input
password()
With Text Input, but typed characters will be appeared as *
GTK may be a better option, as it is cross-platform. It'll work great on Ubuntu, and should work just fine on Windows when GTK and Python bindings are installed.
from gi.repository import Gtk
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO,
Gtk.ButtonsType.OK, "This is an INFO MessageDialog")
dialog.format_secondary_text(
"And this is the secondary text that explains things.")
dialog.run()
print "INFO dialog closed"
You can see other examples here. (pdf)
The arguments passed should be the gtk.window parent (or None), DestroyWithParent, Message type, Message-buttons, title.
You can use win32 library in Python, this is classical example of OK or Cancel.
import win32api
import win32com.client
import pythoncom
result = win32api.MessageBox(None,"Do you want to open a file?", "title",1)
if result == 1:
print 'Ok'
elif result == 2:
print 'cancel'
The collection:
win32api.MessageBox(0,"msgbox", "title")
win32api.MessageBox(0,"ok cancel?", "title",1)
win32api.MessageBox(0,"abort retry ignore?", "title",2)
win32api.MessageBox(0,"yes no cancel?", "title",3)
Start an app as a background process that either has a TCP port bound to localhost, or communicates through a file -- your daemon has the file open, and then you echo "foo" > c:\your\file. After, say, 1 second of no activity, you display the message and truncate the file.