wxpython threading textctrl disappears until files are processed - multithreading

Appropriate code provided below. I had this working once but have since messed up due to having to change some of the other code. Can't figure out what I've done. The encrypt function creates a thread and makes a call to function EncryptProc to process one or more files. Once each file is completed it should print the name to a textctrl. In the codes current state it waits until all threads/files are processed before printing. It then prints evrything in one go. During processing the textctrl also completely disappears. Any help would be much appreciated as its starting to drive me nuts, lol.
---EDIT---
CODE REMOVED

There should be no GUI access from within threads - in your case EncryptProc is writing to the text control directly - you need to either:
Use CallAfter in the thread to update the text control after the thread exits or
Raise custom event an event in the thread that carries the file name information
and have a hander in the main thread that updates the text control
on receiving the event.

Related

What is the best method to tell a function in a tkinter GUI to wait until a system call made by shutils resolves before proceeding?

I have a tkinter GUI that at some point prompts the user for a folder, checks if it exists, and then, if it does and the user consents, deletes and recreates it. I am doing this via the following code:
try:
self.status_string.set('Cleaning up output directory to be overwritten')
shutil.rmtree(output_folder)
while os.path.exists(output_folder):
time.sleep(1)
os.makedirs(output_folder + '/events')
except OSError:
self.status_string.set('Failed to create directory {0}, verify that you have permission to do so'.format(output_folder + '/events'))
I am currently calling time.sleep in order to force it to wait until the directory has been completely removed before trying to recreate it, because the contents can be large and it may take a while and I want to avoid the race condition. But it seems wrong to me to be using sleep during the mainloop of tkinter, and I am not certain that checking for existence after calling rmtree is valid. It seems to work in testing, but that could be luck. What is the proper way to wait for the system call to resolve before proceeding?

How to, in Python, ignore certain/all keypresses while function executing in program on Macintosh

I would like to know, how would I, in Python, be able to ignore certain/all keypresses by the user while a certain function in the program is executing? Cross platform solutions are preferred, but if that is not possible, then I need to know how to do this on Macintosh. Any help is greatly appreciated! :)
EDIT: Right now, I am processing keypresses through the turtle module's onkey() function since I did create a program using turtle. I hope this helps avoid any confusion, and again, any help is greatly appreciated! :)
You might want to modify your question to show how you're currently processing key-presses. For example is this a command-line program and you're using curses?
You could use os.uname to return the os information or sys.platform, if that isn't available. The Python documentation for sys.platform indicates that 'darwin' is returned for OSX apple machines.
If the platform is darwin then you could, in your code, ignore whatever key-presses you want to.
Edit (Update due to changed question):
If you want to ignore key-presses when a certain function is being called you could either use a lock to stop the key-press function call and your particular function being executed together.
Here is an example of using a lock, this may not run, but it should give you a rough idea of what's required.
import _thread
a_lock = _thread.allocate_lock()
def certainFunction():
with a_lock:
print("Here's the code that you don't want to execute at the same time as onSpecificKeyCall()")
def onSpecificKeyCall():
with a_lock:
print("Here's the code that you don't want to execute at the same time as certainFunction()")
Or, depending on the circumstances, when the function which you don't want interrupting with a key press is called, you could call onkey() again, with the specific key to ignore, to call to a function that doesn't do anything. When your particular function has finished, you could call onkey() again to bind the key press to the function that processes the input.
I found similar problems, maybe it will help you with your problem:
globally capture, ignore and send keyevents with python xlib, recognize fake input
How do I 'lock the keyboard' to prevent any more keypresses being sent on X11/Linux/Gnome?
https://askubuntu.com/questions/160522/python-gtk-event-ignore

wxWidgets application loop slideshow

I have a rather simple problem which I can't seem to solve.
I would like to write a slideshow program that also plays an audio file everytime the slide has changed. This audio files vary in lengths and I do not want to program to loop through the next entry / picture till the sound has finished playing.
Currently I have implemented a loop:
void UI_BRAINWASH::PlaySound_top()
{
wxString tmppath(parent->get_currentdirect()+parent->current_db.get_card(m_index)->get_topentryaudiopath());
ISound* firstsound = this->engine->play2D(tmppath.mb_str(), false, false, true);
while(engine->isCurrentlyPlaying(tmppath.mb_str()))
{
StaticTextTop->GetParent()->Update();
//wxSleep(3);
}
m_timer->Start(1000);
}
and this loops through the entries as expected and everything is dandy...
However, I would like, to be able to abort the programm by pressing the Escape amongst other things, but the while loop obviously hinders me from doing exactly that.
I also noticed that I can't move my window or close the programm while it is looping through the pictures.
So I have looked at threads and the wxIdleevent class. in: wxwidgets/samples/threads/ is an example of a "workers thread", which seems to be what I need.
My question now is: are threads not a bit of an overkill for a simple slideshow?
Is there another / better way of looping through my entries - waiting for the sound to have played, updating the gui and also being able to still move the window around?
What is engine?
Most APIs for playing sounds provide the ability to start playing a sound-file and then return immediatly. They will send an event when the sound is finished. They will also provide a call to interrupt a sound that is still playing. This is what you want.
You should check the docs for whatever API you are using and find this feature. If the feature is not available, than you need to find another API that does - most do.

Instantiating a new WX Python GUI from spawn thread

I have main thread that runs a WX Python GUI. The main GUI gives a user a graphical interface to select scripts (which are mapped to python functions) and a 'Start' button, which spawns a second thread (Thread #2) to execute the selected scripts. The problem I am having is I cannot get a new WX GUI (GUI2) to popup and give the user the ability to enter data.
# Function that gets invoked by Thread #2
def scriptFunction():
# Code to instantiate GUI2; GUI2 contains wx.TextCtrl fields and a 'Done' button;
# When 'Done' button is clicked, data entered in fields are process and second GUI is destroyed
gui2Frame = Gui2Frame(None, "Enter Data Gui")
gui2Frame.Show(True)
# This is where I need help. Cannot figure out how to pend for user input;
# In this example; my 'Done' button sets the Event flag
verifyEvent = threading.Event()
verifyEvent.wait(10)
# Process entered data time
processData()
Currently, this approach locks up the GUI2 for 10sec. Which makes sense, because the spawned thread is locked up. Implementing a while-loop with a time.sleep(), does the same. I looked into spawning a third thread, just to handle GUI2, but again, I get back into not knowing how to hold GUI2 active. Any suggestions? Also, please no recommendations about changing the multithreading architecture into one thread; Thank you.
You can't have two wxPython mainloops in one program. You really do have to use just the first wxPython program's main thread. If you want to spawn another application, then use subprocess. Something like this should work:
import subprocess
subprocess.Popen("python path/to/myscript.py")

VC++ - Asynchronous Thread

I am working on VC++ project, in that my application process a file from input path and generates 3 output "*.DAT" files in the destination path. I will FTP these DAT file to the destination server. After FTP, I need to delete only two output .DAT files the folder. I am able to delete those files, because there one Asynchronous thread running behind the process. Since the thread is running, while deleting it says, "Cannot delete, the file is used by another person".
I need to stop that thread and delete the files. Multiple files can also be taken from the input path to process.
Please help me in resolving this issue. Its very high priority issue for me. Please help me ASAP.
I don't think this is a threading issue. Instead I think your problem is that Windows won't let you delete a file that still has open handles referencing it. Make sure that you call CloseHandle on handles to the file that you want to delete first. Also ensure that whatever mechanism you are using to perform the FTP transfer doesn't have any handles open to the file you want to delete.
I don't think that forcing the background thread down will solve your problem. You can't delete the files because you're holding an open handle to those files. You must close the handle first. Create an event object and share it between your main thread and the background thread. When the background thread is done sending the files through FTP, it should set this event. Have your main thread wait on the event before deleting the files.
Background Thread:
SendFiles();
ReleaseResources(); // (might be necessary, depending on your design)
SetEvent( hFilesSentEvent );
Main Thread:
WaitForSingleObject( hFilesSentEvent );
DeleteFiles();

Resources