Created
October 24, 2019 11:55
-
-
Save BenedictWilkins/b84eddb9f7a74ececfe867c8e610a451 to your computer and use it in GitHub Desktop.
test
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 Fri Oct 18 10:27:51 2019 | |
Incorrect example use of tkinter with Python threading | |
@author: Benedict Wilkins AI | |
""" | |
import tkinter as tk | |
from threading import Thread | |
import time | |
t = lambda: int(round(time.time() * 1000)) | |
class Sleep: | |
def __init__(self, wait): | |
self.wait = wait | |
def __enter__(self): | |
self.start = t() | |
self.finish = self.start + self.wait | |
def __exit__(self, type, value, traceback): | |
while t() < self.finish: | |
time.sleep(1./1000.) | |
def quit(): | |
global finish | |
finish = True | |
root.destroy() | |
print("DESTROYED") | |
def run(): | |
x = 0 | |
global finish | |
while not finish: | |
with Sleep(100): | |
root.after(0, lambda : canvas.delete('all')) | |
print("CLOSE NOW!") | |
time.sleep(1) | |
print("RUN AFTER") | |
root.after(0, lambda : canvas.create_rectangle(x,0,x+200,200, fill='red')) | |
x += 10 | |
print("CONTROL DONE") | |
root = tk.Tk() | |
root.title("My Title") | |
root.protocol("WM_DELETE_WINDOW", quit) | |
canvas = tk.Canvas(root, width=400, height=200, bd=0, | |
highlightthickness=0, bg='white') | |
canvas.pack() | |
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