Forum Archive

How to send 'CTRL-C' to console, when close button in ui is tapped?

FrankenApps

I have built an app with bluetooth running in a seperate thread. Now when I close the app via the UI (with the close button in the top left corner), the app still runs and I need to send 'CTRL-C' via the [x] button in Pythonista.
Is there a way to send 'CTRL-C' via the UI close button in the title bar?

I want to call

except KeyboardInterrupt:
  cb.reset()

when the close button in the title bar is tapped.

I have tried making the main View a customView and then using
class MainView(ui.View): def will_close(self): print('Closing') cb.reset() sys.exit(0)
but while the function gets called, I still have to hit the [x] button in order to stop the script execution.

Only to be clear: I do not want to close Pythonista, I only want to stop the script from running, when I hit the close button in the UI and return to the console/editor.

cvp

@FrankenApps How do you present your view? Normally, if you present with hide_title_bar=False, the x button at top left is the standard close button, thus your ui.View closes.
Don't call sys.exit

FrankenApps

@cvp I do it with:

v=ui.loadView()
v.present(style=full_screen, orientations= ['portrait'])

the title bar is shown by default, isn't it?
I can post the whole code later if necessary...

cvp

@FrankenApps it is ok and then, when you tap the x, will_close is called, no?

FrankenApps

Yes, but the Script is still running, even so the UI is closed

cvp

@FrankenApps understood, you have a thread still running...
You have to "warn" it to close

see here

But you have to set, in will_close, a global checked by your thread to see that close has been asked and then leave the thread run by a return