I'd like to have a python program alert me when it has completed its task by making a beep noise. Currently, I use import os and then use a command line speech program to say "Process complete". I much rather it be a simple "bell."
I know that there's a function that can be used in Cocoa apps, NSBeep, but I don't think that has much anything to do with this.
I've also tried
print(\a)
but that didn't work.
I'm using a Mac, if you couldn't tell by my Cocoa comment, so that may help.
Have you tried :
import sys
sys.stdout.write('\a')
sys.stdout.flush()
That works for me here on Mac OS 10.5
Actually, I think your original attempt works also with a little modification:
print('\a')
(You just need the single quotes around the character sequence).
If you have PyObjC (the Python - Objective-C bridge) installed or are running on OS X 10.5's system python (which ships with PyObjC), you can do
from AppKit import NSBeep
NSBeep()
to play the system alert.
I tried the mixer from the pygame module, and it works fine. First install the module:
$ sudo apt-get install python-pygame
Then in the program, write this:
from pygame import mixer
mixer.init() #you must initialize the mixer
alert=mixer.Sound('bell.wav')
alert.play()
With pygame you have a lot of customization options, which you may additionally experiment with.
I had to turn off the "Silence terminal bell" option in my active Terminal Profile in iTerm for print('\a') to work. It seemed to work fine by default in Terminal.
You can also use the Mac module Carbon.Snd to play the system beep:
>>> import Carbon.Snd
>>> Carbon.Snd.SysBeep(1)
>>>
The Carbon modules don't have any documentation, so I had to use help(Carbon.Snd) to see what functions were available. It seems to be a direct interface onto Carbon, so the docs on Apple Developer Connection probably help.
Building on Barry Wark's answer...
NSBeep() from AppKit works fine, but also makes the terminal/app icon in the taskbar jump.
A few extra lines with NSSound() avoids that and gives the opportunity to use another sound:
from AppKit import NSSound
#prepare sound:
sound = NSSound.alloc()
sound.initWithContentsOfFile_byReference_('/System/Library/Sounds/Ping.aiff', True)
#rewind and play whenever you need it:
sound.stop() #rewind
sound.play()
Standard sound files can be found via commandline locate /System/Library/Sounds/*.aiff
The file used by NSBeep() seems to be '/System/Library/Sounds/Funk.aiff'
By the way: there is a module for that. ;-)
Just install via pip:
pip3 install mac_alerts
run your sound:
from mac_alerts import alerts
alerts.play_error() # plays an error sound
Play sound worked for me. Install using pip
pip3 install playsound
To play sound
from playsound import playsound
playsound('beep.wav')
References:
Found the examples here
downloaded beep.wav from here
Related
When I launch the script and press the "]" the script closes. And I know that it works because I tried it in IDLE and it works fine and the music is playing as intended.
import keyboard
from playsound import playsound
while True:
if keyboard.is_pressed(']'):
sound = playsound(r'C:\Users\jaros\Downloads\DoomMusic.mp3')
I tried using "time.sleep()" and "Input()" but nothing worked. The script just keeps closing whenever I press the "]" key.
Solution
To use the "keyboard" package properly, do it like this:
import keyboard
from playsound import playsound
while True:
keyboard.wait("]")
playsound(r"C:\Users\jaros\Downloads\DoomMusic.mp3")
Rationale
Both of the packages you reference (keyboard and playsound) seem to be third-party and not part of the standard library.
Playsound Is Old
Please be advised that the playsound package seems old and broken; the release on PyPI is from March 2020 and needs to be updated by the package maintainers. Pip version 22.3.1 prints a deprecation warning on installation:
DEPRECATION: playsound is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
The "playsound" module seems also seems to be broken on Windows, but working on macOS. On Windows 10 I am not able to make it work. I get horrible error messages of the type:
>>> playsound(r"C:\Users\traal\Downloads\Garry Schyman - Praan.mp3")
Error 277 for command:
open "C:\Users\traal\AppData\Local\Temp\PS0vt8vkrq.mp3"
A problem occurred in initializing MCI.
The source code for playsound states that it was tested on Windows 7 and Python 2.7 -- these are super old versions.
I would recommend looking for a different package than playsound, unless the package maintainers release an updated version soon.
Proper Use of the Keyboard Package
I googled the keyboard package, and it seems that the way you're using it is an anti-pattern. According to the documentation:
import keyboard
# Don't do this!
#
#while True:
# if keyboard.is_pressed('space'):
# print('space was pressed!')
#
# This will use 100% of your CPU and print the message many times.
# Do this instead
while True:
keyboard.wait('space')
print('space was pressed! Waiting on it again...')
# or this
keyboard.add_hotkey('space', lambda: print('space was pressed!'))
keyboard.wait()
I'm trying to copy some text to clipboad in my python program, so I've installed pyperclip via pip command via Windows command line interface, it says everything is successfully installed, not a problem. However, when I import the thing into my project, I get
from tkinter import *
from tkinter import ttk
import pyperclip
import binascii
#my code...
...
ModuleNotFoundError: No module named 'pyperclip'
So I was like *** that, maybe it's broken, I found a little library that does exactly the same, called "clipboard". Exactly the same installation procedure - from the command line. Same successful installation. Same ModuleNotFoundError. So clearly something wrong on my side, but I have no idea what it is, no idea where to look and what to do. I just want to be able to copy some text to clipboard. Multiplatform. That's it.
It's Python 3.8, Windows 10
pyperclip-1.8.0
pip-20.2.2
If I need to show you some logs or tell you something about my installation, please tell me what exactly to do and where to find information you may need. I'm good with instructions, but I don't have much (any) experience rummaging through logs and python installation folders.
Similar posts have slightly different problems, I didn't find any clear solution or hints that I could understand. No reply seemed like a solution to me.
Anyway, I would really appreciate any help from the community. Being unable to simply copy something to clipboard is killing me, especially since it's the only thing I can't implement. And it's just one function for one button to copy one short piece of text. It's like being stuck at 99% loading, when everything is done, and you just can't write the final line of code, haha.
Pycharm uses a virtualenv for pycharm projects. Navigate through:
Pycharm >> File >> Settings(or press Ctrl+Alt+S on win) >> Project:Name >> Project Interpreter >> Click on the plus(+) symbol above the scroll bar and type in pyperclip and press install package.
And this will install pyperclip for your Pycharm IDE. Since pycharm uses a virtual env for its projects you can change it in the project interpreter section as well, and then the pyperclip you installed from the cmd using pip, will be available to be used on that global version of python(now used by your Pycharm too).
If you still have any errors or doubts, do let me know :D
Cheers
So, i installed some stuff for python, and going through all my imports and trying to pip install them gives me a 'Requirement already satisfied'. When I run a script though, everytime i debug to
import vlc
I get a
OSError: [WinError 126] The specified module could not be found
directly instead of passing through to the next import. if i comment out this import, it works up to the point where I use it. I am using VSCode, my python version is set to 3.7.1, so that is not the problem. I rarely work with python, so I do not know what else I can do at this point.
While obvious to some, I did not know that VLC was Traffic Cone (tm) video player, and probably should have read the vlc docs.
You need to have the actual VLC installed on you PC for the vlc import to work with python
I want to have python save an image file of the whole screen as a variable with ctypes so that I could access the screen in a program and do something with it (like put it on a pygame window). I can't use any other libraries unless they are included with python (no installing or pip). Does anyone know how to do this?
Edit: I'm using windows 10.
PIL.ImageGrab is from PILLOW (a python image library fork which you can install with pip). You can give a bounding box or capture the entire screen.
Update: OP now mentions he can't use external libraries.
Then you could virtually hit printscreen and read the clipboard. The code of PILLOW is open-source feel free to use it.
Remember that you can always call a command from within python:
>>> import os
>>> os.system("pip install pillow")
Or download the zip of the library and import it in your code.
With python on windows, I have been making a game. However, this game requires the msvcrt module, which is only available on windows. I need the function msvcrt.getch(). If I was to make it possible to run this game on Ubuntu, or any linux computer in general, what module, if any, would I be able to use? I would be fine with it not working on a linux, but I would really like to find out a way. Again, is there a module, or any tools I can use, to use the msvcrt.getch function on ubuntu?
The library msvcrs is only available on Windows. If you want to use a release for Ubuntu, check getch function.
import getch
This function get the pressed-key in keyboard.
Hope it help!
From the command line, if necessary (as Luan Souza said above):
pip install getch
In your code:
try:
from getch import getch, getche # Linux
except ImportError:
from msvcrt import getch, getche # Windows