Apr-29-2022, 10:37 AM
(Apr-27-2022, 06:58 PM)Gribouillis Wrote:Instead of another new function I implemented the try except block inside the ThreadAutoRender class like so:philipbergwerf Wrote:What happens if there is a python error inside the render() function because I did not fix all bugs?A simple solution for this is to call instead a new functionrender_no_error()
that cannot raise an exception. This function would callrender()
def render_no_error(): try: render() except Exception as e: pass # handle the exception the way you wantThe drawback of this technique is to silently suppress exceptions, which makes debugging hard. You will need to leave a trace of the exceptions somewhere, perhaps on stderr or better in a log file or perhaps display it in a tkinter window. You could also catch specific exception types before the Exception type.
philipbergwerf Wrote:it is becoming annoying because it does this drawing every time you press a keyI have been using for a long time a Python program to write mathematical documents. I type in an editor and a subprocess parses what I type and calls other programs to create a Pdf file. I see two windows: the editor and the pdf viewer. The event that triggers the recalculation ot the pdf file is not every keystroke. Instead I recalculate the pdf file every time I typeCtrl-S
in the editor to save the source file. This is a very fluid workflow. I suggest that you bind a sequence such asCtrl-S
to the Text or the root widget that would simultaneously save your work and redraw the canvas. It allows you to type as many characters as you want without triggering a redraw.
I'm not a very good tkinter programmer because I don't use it very often but I can imagine solutions to your idea of hiding the canvas when you don't want to see the computer drawing. For example the canvas could be on a page of a Notebook widget and you would simply display another empty page.
class QuitThread(Exception): pass class ThreadAutoRender(Thread): def __init__(self): Thread.__init__(self) self.needs_to_render = False self.condition = Condition() def run(self): try: while True: self.wait_and_render() except QuitThread: pass def wait_and_render(self): with self.condition: if not program_is_running: raise QuitThread while not self.needs_to_render: self.condition.wait() if not program_is_running: raise QuitThread self.needs_to_render = False try: render() except Exception as e: print('error: ', e) thread_auto_render = ThreadAutoRender() thread_auto_render.start()