Multithreaded Music Playing with Pygame in Terminal - python-3.x

I am attempting to make a text game and add some music to it and maybe sounds too, however, the "Big and Greatest" answer was Pygame. After testing with it, I found that when you use ANY Pygame function, it stops the current terminal session from printing any more output, this stops the game from continuing.
I first tried the usual...
from pygame import *
def some_function():
mixer.load("music.mp3")
mixer.music.play(0)
clock = time.Clock()
clock.tick(10)
while mixer.music.get_busy():
clock.tick(10)
Rest_of_function
And of course this does not work because it is a loop and is doing it in order so I next tried...
from file_with_music import * #added threading to this file
def main():
file_with_music.start()
Rest_of_Function
This however did not work as the annoying "Welcome to pygame" popup blocked terminal output again...
SO the next step was to make a new file and...
from Main import *
from Intro_Sounds import *
if __name__ == '__main__':
Intro_Thread.start() #Intro did not start because pygame popup was somehow called
Music_Thread.start()
Even after putting them both in threads, it still did not work...
As a side note, there is a function built into Main that is meant to execute
Music_Thread.join()
Music_Thread.stop()
After certain input is met.
The expected result is for it to play music in the background while the terminal continues printing out input and accepting input up until the input that would kill the Music thread is input. Of course, it did not work, and I am at a loss...
How can music be played without freezing the terminal.

The pygame.mixer sadly doesn't have this capability (as far as I know). Use winsound instead:
import winsound # only on windows tho...
winsound.PlaySound('sound.wav', winsound.SND_ASYNC) # this will play the sound and...
print('I can still print :D') # this can still print :D

After trying a few things I found...
Pygame is not friendly with sound and terminal output, so it won't work.
WxPython no longer has a functional sound module, making it useless.
Other projects are abandoned or OS specific...
However, I found that PyOpenAL actually works, although it can't understand mp3 files or 32bit float wav. Instead of using multithreading I switched to multiprocessing (making a 2000 line script to properly learn how the module works) and now the sound plays (and loops) while allowing terminal output.
TL:DR
PyOpenAL is the winner.

please try as follow--
from pygame import *
def some_function():
pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play(0)
clock = time.Clock()
clock.tick(10)
while pygame.mixer.music.get_busy():
clock.tick(10)
Rest_of_function

Related

How to manipulate tkinter GUI from an imported file

I try to create a small GUI with tkinter. To make my code more readable I want to split the code in 2 files. One for the GUI information and one for the process informations. Or is it a bad idea?
So I create a gui.py where I import my process informations from program.py.
gui.py:
import tkinter as tk
from program import *
root = tk.Tk()
btn_Start = tk.Button(text="Start", command=start_loop)
btn_Stop = tk.Button(text="Stop", command=stop_loop, state=tk.DISABLED)
btn_Start.grid(row=1, column=0)
btn_Stop.grid(row=1, column=1)
root.mainloop()
program.py:
def start_loop():
print('disable Start button and enable Stop button')
# what is the code to disable the start button and enable the stop button?
def stop_loop():
print('disable Stop button and enable Start button')
# what is the code to disable the stop button and enable the start button?
How do I tell the button the disable/enable information in my program.py file? I do not understand how I get the information from the gui to the program and back to the gui?
Thanks for your help
For such a small program, it is overkill.
Looking at the tkinter programs I've written, all of them are between 200−300 lines. This is including headers, comments and blank lines. The number of actual code lines is 100−200.
In my opinion that is small enough to comfortably handle in an editor in one file.
Looking over most of my source code repositories, the longest Python files tend to top out at around 230 lines of actual code.
Keeping all the code in one file has significant advantages for a program when you need to install it. Just copy the file and you're done. No need for modules and a setup.py.

Python program stops right away

So I tried to record they keystrokes of my keyboard, when I use the python IDLE and write it in there, it works, but when i run it in the terminal with python3.8.1 it ends right away. That's the code:
import keyboard
def on_key(key):
print(key)
keyboard.on_press(on_key, suppress=False)

Python doesnt respond after calling filedialog.askopenfilename() from TK

this is my first question:
Im looking to implement a interactive way to look for a path and then load the data frame with pandas.
Im using Tk, when i run the code it seems like the terminal is running a infinite loop.
`import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path_to_open = filedialog.askopenfilename()`
where is my error ? any advice? im looking for something similar to uigetfile from MATLAB in python.
I don't know whether this answer is still useful for you, but maybe for those people having the same issue and coming here via google:
The issue is not caused by the piece of code itself, because that is working fine. See also Quick and easy file dialog in Python?
I assume it is caused because of some background tk processes which were not closed cleanly.
My solution was to restart the python kernel (using Jupyter Notebook with python 3.6) and reimporting. Also, it might help to close all background python processes, which might be still running, via the task manager.

Making Python text green and using Spinning cursor - Newbies questions

I want to make my Python script file,when I "announce" something to the user, to be green like this:
How can this be done?I saw a script using this with sys.stdout.write but I dont understand how to use it, Im using a simple "print" commands..
Also, I would like to have the Spinning cursor spin as long as this command runs and only stops when this command stops(finishes):
print('running network scan')
output = subprocesss.check_output('nmap -sL 192.168.1.0/24',shell=True)
print('Done')
Any way to do that (unknown time until task is done)?
Im using a code suggested by nos here:
Spinning Cursor
So, about getting the terminal color to be green, there is a neat package called colorama that generally works great for me. To check whether the process is running or not, I would recommend using Popen instead of check_output, since the latter does not allow you to communicate with the process as far as I know. But you need to since you want to know if your subprocess is still running. Here is a little code example that should get you running:
import subprocess
import shlex
import time
import sys
import colorama
def spinning_cursor():
"""Spinner taken from http://stackoverflow.com/questions/4995733/how-to-create-a-spinning-command-line-cursor-using-python/4995896#4995896."""
while True:
for cursor in '|/-\\':
yield cursor
# Create spinner
spinner = spinning_cursor()
# Print and change color to green
print(colorama.Fore.GREEN + 'running network scan')
# Define command we want to run
cmd = 'your command goes here'
# Split args for POpen
args=shlex.split(cmd)
# Create subprocess
p = subprocess.Popen(args,stdout=subprocess.PIPE)
# Check if process is still running
while p.poll()==None:
# Print spinner
sys.stdout.write(spinner.next())
sys.stdout.flush()
sys.stdout.write('\b')
print('Done')
# Grab output
output=p.communicate()[0]
# Reset color (otherwise your terminal is green)
print(colorama.Style.RESET_ALL)

Python colorama not working with input?

Finally got colorama working today, and it works excellent when printing strings, but I got the common error everyone seems to get when I attempted to use colorama with input.
Here's my code:
launch = input(Fore.GREEN + "Launch attack?(Y/N): ")
Screenshot of output:
I had this same issue (Python 3.5.4) and, just in case it is not too obvious for somebody else looking at this, you can always rely on the workaround of combining print / input calls where you previously had just an input call:
print(Fore.GREEN + "Launch attack?(Y/N): ", end='')
launch = input()
This should produce the exact same output as in your question, with no extra blank lines and with the code coloring working without the need of importing anything else.
The (small?) disadvantage is that you you will end up with two lines of code where you previously had just one.
On my system, input() works with colors if you add
import sphinx.quickstart
to your module.
So here is the full code.
from colorama import Fore
import colorama
import sphinx.quickstart
colorama.init()
launch = input(Fore.GREEN + "Launch attack? (Y/N): ")
(This leads to two questions:
Why does it not work in the first place?
What is the actual reason? – Someone might like to dive into the sphinx source code.)
N.B. if you run python via winpty from Git Bash, set convert.
colorama.init(convert=True)
Otherwise, you do not get color with the current versions.
To get rid of this problem in the starting of the code add
import os
os.system('cls')
This will clear the screen and hence clear all the external factors blocking the colorama in input.
This works 100% you just need to do it once in the starting of the program [or just before the first use of colorama with input] and after that use colorama in any creative way you want to.
I hope this will help all the creative minds trying to make their codes more colourful
just make sure about the 'autoreset' in init()
init(autoreset=True)

Resources