def test_error_in_thread_call(deinit_threads, handy_callback): teek.init_threads() @handy_callback def thread_target(): with pytest.raises(teek.TclError) as error: teek.tcl_eval(None, "expr {1/0}") exc = error.value assert isinstance(exc, teek.TclError) assert exc.__traceback__ is not None # error_message is the traceback that python would display if this # error wasn't caught error_message = ''.join( traceback.format_exception(type(exc), exc, exc.__traceback__)) assert error_message.startswith("Traceback (most recent call last):\n") regex = (r'\n' r' File ".*test_threads\.py", line \d+, in thread_target\n' r' teek\.tcl_eval\(None, "expr {1/0}"\)\n') assert re.search(regex, error_message) is not None thread = threading.Thread(target=thread_target) thread.start() teek.after(100, teek.quit) teek.run() thread.join() assert thread_target.ran_once()
def test_wait(): var = teek.StringVar() start = time.time() teek.after(500, functools.partial(var.set, "boo")) var.wait() # should run the event loop ==> after callback works end = time.time() assert (end - start) > 0.5
def test_wait_window(): window = teek.Window() start = time.time() teek.after(500, window.destroy) window.wait_window() end = time.time() assert end - start > 0.5
def test_empty_tuple_bug(): # after half a second, press escape in the widget of the dialog that # happens to be focused teek.after(500, lambda: teek.tcl_eval( None, "event generate [focus] <Escape>")) # do the dialog, tkinter should return an empty tuple which should be # converted to an empty string result = teek.tcl_call(str, 'tk_getSaveFile') # this threw an error assert result == ''
def enter(self, event): # For some reason, toplevels get also notified of their # childrens' events. if event.widget is self.widget: self.destroy_tipwindow() self.got_mouse = True teek.after(1000, self.show) # these are important, it's possible to enter without mouse move self.mousex = event.rootx self.mousey = event.rooty
def test_progressbar_bouncing(): progress_bar = teek.Progressbar(teek.Window(), mode='indeterminate') assert progress_bar.config['value'] == 0 progress_bar.start() def done_callback(): try: # sometimes the value gets set to 2.0 on this vm, so this works assert progress_bar.config['value'] > 1 progress_bar.stop() # prevents funny tk errors finally: # if this doesn't run, the test freezes teek.quit() teek.after(500, done_callback) teek.run()
def test_make_thread_safe(handy_callback, deinit_threads): @teek.make_thread_safe @handy_callback def thread_target(): assert threading.current_thread() is threading.main_thread() teek.init_threads() thread = threading.Thread(target=thread_target) thread.start() # make_thread_safe needs teek.run to work teek.after(500, teek.quit) teek.run() assert not thread.is_alive() assert thread_target.ran_once()
def test_cancel(): timeout = teek.after(1000, print, args=["it didn't work"]) timeout.cancel() assert repr(timeout).startswith("<cancelled 'print' timeout") with pytest.raises(RuntimeError) as error: timeout.cancel() assert str(error.value) == "cannot cancel a cancelled timeout" def try_to_cancel_the_completed_timeout(): with pytest.raises(RuntimeError) as error: timeout.cancel() assert str(error.value) == ("cannot cancel a successfully " + "completed timeout") timeout = teek.after_idle(lambda: None) teek.after(50, try_to_cancel_the_completed_timeout) teek.after(100, teek.quit) teek.run()
def test_after(): start = time.time() timeout = teek.after(200, teek.quit) assert repr(timeout).startswith("<pending 'quit' timeout") teek.run() end = time.time() # the upper bound used to be 0.21, but it failed once # then i tried 0.22 and after a while that failed too assert 0.20 < (end - start) < 0.25 assert repr(timeout).startswith("<successfully completed 'quit' timeout")
def test_basic_stuff(deinit_threads, handy_callback): teek.init_threads() text = teek.Text(teek.Window()) def thread_target(): for i in (1, 2, 3): text.insert(text.end, 'hello %d\n' % i) thread = threading.Thread(target=thread_target) thread.start() @handy_callback def done_callback(): assert text.get(text.start, text.end) == 'hello 1\nhello 2\nhello 3\n' teek.quit() # i experimented with different values: 500 was enough and 450 wasn't, so # this should be plenty teek.after(1000, done_callback) teek.run() thread.join() assert done_callback.ran_once()
def start(self): if self.timeout is None: self.timeout = teek.after(3000, self.callback) print("running callback after 3 seconds") else: print("already started")
def updater_callback(self): self.label.config['text'] = time.asctime() # tell tk to run this again after 1 second teek.after(1000, self.updater_callback)