I am making a simple Python utility that shows the tempo of a song (BPM) that is playing. I record short fragments of a few seconds to calculate the tempo over. The problem is that now I want to show this on a display using a Pygame UI, but when I'm recording sound, the UI does not respond. I want to make it so that the UI will stay responsive during the recording of the sound, and then update the value on the screen once the tempo over a new fragment has been calculated. How can I implement this?
I have looked at threading but I'm not sure this is the appropriate solution for this.
I'd use the python threading library.
Use the pygame module in the main thread (just the normal python shell, effectively) an create a separate thread for the function that determines BPM.
This BPM can then be saved to a global variable that can be accessed by PyGame for displaying.
Related
I am making turn based game in python using Pyglet. The game has a player-vs-AI mode in which the bot calculates a move to play against the player. However, the function which calculates the bot's move takes around 3-5 seconds to run, blocking the game's UI. In order to get around this, I am running the bot's calculation on a second process using multiprocessing.Process. I got it to work well without blocking the UI, however every time I open the second process to run the function a new Pyglet window opens, then closes again when the process is closed. Is there any way to open a second process in a Pyglet program without a second window opening? Let me know if examples of my code is required, and I will try to come up with similar code to share. Thanks in advance to anyone who can help.
You can fix the problem by moving the initialization of the window inside of the main block
I was experimenting with pygame and noticed it raised a VideoExpose event when I press alt+tab and the window is fullscreen. when I switch press alt+tab again, everything on the screen is moved to the bottom left.
I know that this is supposed to mean that 'portions of the window must be redrawn', but how am I supposed to redraw them and what why does pygame even have this event in the first place?
If you are writing a program to use the windowing Event Model, the windowing environment sends the program events to notify it of environmental changes - window resize, mouse move, need to re-paint, etc.
Not handling these events will cause your application to be considered "non responsive" by the environment. Here on SO, there's about one question a week with PyGame and exactly this issue.
When working with PyGame re-drawing event handling may seem superfluous as the majority of PyGame games redraw the entire screen every frame, e.g.: 60 FPS. But if unnecessary, this method is a complete waste of resources (CPU-time, electricity, etc.) It is quite simple though, so good for beginners.
Say you were writing a card game like Solitaire... the screen updates only when interacting with the user. In terms of CPU, it's doing nothing 99.9% of the time while the user contemplates their next move. In this case, the program could be written to only re-draw the screen when necessary. When is it necessary? When the player gives input, or the program receives a pygame.VIDEOEXPOSE event from the widowing environment.
If your program is redrawing the window constantly, you can simply ignore the message. If not, when receiving the message call whatever block of code is normally used to render the window. The expose message may come with the region of the screen that needs to be re-drawn, in this case a really good application would only update that section of the display.
can I create/push custom event in Tkinter python?
in the code https://stackoverflow.com/a/10575137/933882
the redraw callback is still invoked on a fixed schedule, every 100 milliseconds etc.
but is it possible to trigger the callback immediately as the external serial data is available ? from usage point of view it's probably not much different, but the mechanisms are very different.
thanks
I am writing a code which loads some images and music files for their usage in the code .
Problem is that it goes to not responding state for few seconds when i run program for the first time in windows , I know it should take some time to load images and music however i want to do that without going to not responding state but at a time when i can show a screen named loading and on background all stuff gets loaded and once stuff is loaded program should go further .
Any ideas ?
You should be able to make the splash screen visible, although I am new to kivy so I don't know how. In some SDK you can control when the screen disappears, you could make it disappear after your media are loaded. But sometimes this will mean too long a wait for UI to appear, in that case you are better off doing as #inclement suggests in Remove or replace the kivy splash screen on startup:
make sure your build method doesn't do much, and try to construct as much state as possible only after that.
You would typically load the media in a separate thread that you start from the GUI build method, and you have your thread signal load complete via a callback where you update GUI accordingly. I don't know yet if that is how one is meant to do it in kivy.
I'm developing a media player. Right now it's a simple window with a button to load .wav files. The problem is I would like to implement a pause button now. But, when playing a audio file the GUI isn't accessible again (no buttons can be pushed) till the file is done playing. How can I make the GUI dynamic while an audio file is playing?
I'm using PyAudio, and their implementation doesn't allow this.
Probably you have to use threads for that. You have to play your audio file in a different thread than the gui mainloop so that the GUI keeps responding user input.
IMHO, wxpython is not so complicated and has some utility functions that would help to do what you want. Check the wxpython demo, you have several examples there.
You can alternatively use pygame mixer for the purpose , I made the same in pyqt and I did'nt require to implement threading . You can get the documentation of pygame mixer at https://www.pygame.org/docs/ref/mixer.html
Happy Coding .
Try this out:
Check the code https://drive.google.com/file/d/0B7ccI33Aew5fNVhwZ2puYTBuUFU/view?usp=sharing
I have used pygame also.Hope this helps.