I've been working on a media player in python that uses pygame.mixer to play music and PyQt4 to build the UI.
I'm currently using a while loop to check if a song is finished so the next song can be loaded in from the queue. In order to make sure that the user can still interact with the PyQt buttons while the while loop is running, I have put QtCore.QCoreApplication.processEvents() into the while loop.
However, now when I close the main window while music is playing, the program does not stop (as it normally does) and the music keeps playing.
The program also can't detect the app.aboutToQuit.connect() command while the music is playing (meaning it can when music isn't playing).
Any help would be greatly appreciated.
I found the answer to this issue a while ago so thought I should put it here for anyone else.
I gave the program an internal exit button in the ui, then made the button call os._exit(0). It worked perfectly then.
Checking the Python and PyQt documentation gave no comment on closing a application window while a while loop is being performed.
Any other solutions would be appreciated though.
Related
I have simple program with only one function which process files and then converts to pdf on a press of button. initially GUI window freezes while the function was running, after this I introduced threading into the program and now GUI freezing issue was resolved, but now when press the button again it shows below error
if anyone can help me on how to fix this and also explain how it works, that will be really helpful, as I am new to python and just started using Tkinter.
sample code would be even more helpful.
You shouldn't use the old thread object you used on the first button click. Instantiate a new one instead.
This is because, you can't start the same thread twice.
So maybe keep a list of threads and add a new one with each button click.
EDIT:
I apparantly wasn't aware of daemon threads.. This solved the main issue..
Now just puzzling to keep the loop checking for button press, whether it has been pressed or not =)
I'm trying to stop a raspberry pi running code when I press a button (emergency stop button connected to the pins).
So far it's not working out. (In my example code I use code to switch the light of the button to make testing easier).
The threading seems to be working well, however the shell program installed in the pi running the code, first 'test runs' the code to check for errors before actually executing. In my while not read_button() code, the 'checking' of the code gets stuck on the 'while not' until I press the button; not ideal..
Reversing the code and having while read_button() or only if read_button() doesn't actually respond to my button press, since likely the code finishes after checking instead of continuously check.
So.. How to solve this? A continues loop stops my code from proceeding until the while loop is broken (I did join the thread in the end..). A non-continues loop doesn't function, since it won't continuously check for the button press..
To make things worse; after the emergency stop was performed I want the thread to still continue checking for button press after (the user might decide to resume the program and have to hit emergency stop again later on the way within the same code).
I feel like I'm in an endless loop myself trying to fix this =P
(Sorry for the lengthy post and sorry for any noob-terms used, I'm very new to this; only picked programming up 3 weeks ago)
Thank you very much in advance!!
def button_check():
while not read_button(): #developer API
if read_button():
set_button_light(blue=True) #developer API
t = Thread(target=button_check)
t.start()
time.sleep(20) #just to give me time to test
t.join()
I'm trying to write a simple python script to stop the music being played by a Mac. I found some code that emulates the media buttons from the accepted answer here: emulate media key press on Mac.
Triggering the play/pause button works perfectly, but I only want to do so if there is music currently playing. Otherwise it turns on the music (the opposite of what I'm trying to do. Is there any way to get this information from the system?
I need to check if music was actually playing beforehand so I can know whether to resume it later.
If your use case is macOS specific, you can call AppleScript via Python:
import subprocess
subprocess.call(['osascript', '-e', 'tell application "iTunes" to pause'])
I'm using the pygame function pygame.time.delay() to make pygame freeze while presenting some pictures or text, and I want to play a sound at the same time.
The problem, I think, is that in order for pygame mixer to work the main loop must be running, so putting the system on wait it also stops the mixer.
Any easy way to solve this problem? thanks
pygame.time.delay() is a bad idea for waiting while presenting things, it not only will prevent you from playing music it can also freeze your screen (blank screen).
You should process events and update the display although you have static image.
Some alternatives are:
Use a counter in your loop to know how long you should keep the same image in the screen.
Use a timer to receive a User Event that will notify you that you need to change the current state.
I have a Python GUI that uses Tkinter. I have to SSH into another place to get data. I start a new thread to do this so that the GUI doesn't hang. During this time, I want to pop up a screen that lets the user know it is loading. Once the program is finished getting the data, I want to close the loading screen. What must I do to have my main loop recognize that the thread is done? I've tried to use that thread to close the loading screen that exists in the main loop, but then I discovered that doesn't work.
I have seen some producer consumer models that don't use GUIs, and they have while loops. This doesn't help me though. I also don't want to download and install other packages, but imports are ok. Thank you for your help!
Have your thread set a flag when it is done. Have the GUI periodically check for that flag and dismiss the window when it is set.
You can check for the flag by creating a function that checks for the flag, and if it's not set it uses after to have itself run again a few hundred ms later. The window won't go away immediately after the thread exits, but as long as the lag isn't more than a couple hundred ms the user will never notice.