I'm following the documentation https://docs.python.org/3/howto/curses.html to setup a curses app in Python. According to the documentation it is recommended to use the wrapper as it auto initializes everything and handles exceptions.
The example provided is fairly basic by using a single function only. I'd like to use a class to wrap the curses functionality and trigger changes to the TUI. How would I use the wrapper in that scenario? Below is an example of what I've tried so far but running the code outputs the traceback in a weird alignment and puts the terminal in a broken state which then requires a reset.
import curses
from curses import wrapper
class Test:
def __init__(self, stdscr):
self._stdscr = stdscr
@classmethod
def create(cls):
_cls = wrapper(cls)
return _cls
def start(self):
self._screen = curses.newpad(100, 100)
self._screen.addstr(2, 2, 'test title', curses.A_STANDOUT)
screen_rows, screen_cols = self._stdscr.getmaxyx()
self._screen.refresh(0, 0, 0, 0, screen_rows - 1, screen_cols - 1)
while True:
self._stdscr.getch()
1/0 # example of uncaught exception
t = Test.create()
t.start()
Output
Traceback (most recent call last):
File "/tmp/test/test.py", line 27, in <module>
t.start()
File "/tmp/test/test.py", line 23, in start
1/0
~^~
ZeroDivisionError: division by zero
Essentially I'm looking for a solution to handle any uncaught exceptions
You should add a try and except block to handle the zero division error If you add the following in the right spot, your code should work better