Created
October 24, 2019 11:58
-
-
Save BenedictWilkins/48f7863e697dfb7a12a53178f8e71a6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Oct 24 12:55:41 2019 | |
Boiler-plate code for tkinter and Python threading | |
@author: Benedict Wilkins AI | |
""" | |
import tkinter as tk | |
from threading import Thread | |
import time | |
class Sleep: | |
def __init__(self, wait): | |
self.wait = wait | |
def __enter__(self): | |
self.start = self.__t() | |
self.finish = self.start + self.wait | |
def __exit__(self, type, value, traceback): | |
while self.__t() < self.finish: | |
time.sleep(1./1000.) | |
def __t(self): | |
return int(round(time.time() * 1000)) | |
def quit(): | |
global finish | |
finish = True | |
root.destroy() | |
def after(t, fun, *args): | |
global finish | |
if not finish: | |
root.after(t, fun, *args) | |
def run(sleep=100): | |
global finish | |
while not finish: | |
with Sleep(sleep): | |
pass #calls to after(t, fun, *args) | |
root = tk.Tk() | |
root.title("My Title") | |
root.protocol("WM_DELETE_WINDOW", quit) | |
# create some widgets | |
global finish | |
finish = False | |
control_thread = Thread(target=run, daemon=True) | |
control_thread.start() | |
root.mainloop() | |
control_thread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment