Advertisement
furas

Python - tkinter, canvas & threading

May 20th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. import time
  2. import tkinter as tk
  3. import threading
  4.  
  5. #
  6. # PEP 8 - Style Guide for Python Code: https://www.python.org/dev/peps/pep-0008/
  7. #
  8. # PEP8: two empty lines between functions
  9. # PEP8: lower_case_names for functions
  10. # PEP8: more readable variables
  11. # PEP8: all "global" in separated lines at the beginning of function, and empty line at the end
  12. # PEP8: space after comma
  13. # PEP8: spaces around = (but not in function's arguments)
  14.  
  15.  
  16. def print_string(item, string):
  17.     if item:
  18.         for char in string:
  19.             idx = canvas.index(item, tk.END)
  20.             canvas.insert(item, idx, char)
  21.             canvas.update()
  22.             time.sleep(0.01)
  23.  
  24.  
  25. def key_event(label, event):
  26.     global text
  27.     global do_it
  28.     global checkForPassword
  29.     global password
  30.  
  31.     string = 'abcdefghijklmnopqrstuvwxyz'
  32.     text = canvas.itemcget(label, "text")
  33.    
  34.     if do_it is False: # PEP8: for `False`/`True` use `is` instead of `==`/ `!=`
  35.         thread_event.set()
  36.         print('something')
  37.         return True
  38.    
  39.     if event.keysym == "BackSpace":
  40.         canvas.itemconfig(label1, text=text[:-1])
  41.     elif event.keysym == 'space':
  42.         canvas.itemconfig(label1, text=text+' ' )
  43.     elif event.keysym == "Return":
  44.         thread_event.set()
  45.         password = text
  46.         print('password is:', password)
  47.         do_it = False
  48.         check_for_password=True
  49.         print('checkForPassword =', check_for_password)
  50.         print(bool(event))
  51.     elif event.char in string:
  52.         # Character is an ASCII letter
  53.         canvas.itemconfig(label1, text=text+event.char)
  54.  
  55.  
  56. def first_half():
  57.     global canvas
  58.     global label1
  59.     global label2
  60.     global entry
  61.    
  62.     # all variables
  63.     prompt1 = """--------------------------------
  64. WELCOME TO GLOBAL DEFENSE SYSTEM
  65. --------------------------------
  66. PLEASE ENTER THE PASSWORD TO ACTIVATE EMERGENCY SHIELD PROCEDURE:"""
  67.  
  68.  
  69.     canvas = tk.Canvas(root, width=700, height=400, bg='black')
  70.     canvas.pack()
  71.  
  72.     canvas_txt = canvas.create_text(1, 0, fill= 'white', anchor='nw', font=('Courier New', 11))
  73.     canvas.update()
  74.  
  75.     label1 = canvas.create_text(1, 68, text=text, fill='white', anchor='nw', font=('Courier New', 12))
  76.     label2 = canvas.create_text(1, 72, text='', fill='white', anchor='nw', font=('Courier New', 11))
  77.  
  78.     print_string(canvas_txt, prompt1)
  79.  
  80.     root.bind('<Key>', lambda e: key_event(label1, e))
  81.  
  82.     root.update()
  83.  
  84.  
  85. def second_half():
  86.    
  87.     thread_event.wait()
  88.  
  89.     if password == 'united states':
  90.         canvas.itemconfig(label2, text='CORRECT PASSWORD')
  91.     else:
  92.         canvas.itemconfig(label2, text='INCORRECT PASSWORD: ' + password)
  93.  
  94.     root.update()
  95.  
  96.  
  97. def main_function_with_extra_threading():
  98.     global root
  99.    
  100.     root = tk.Tk()
  101.    
  102.     the_first_half_threading = threading.Thread(target=first_half)
  103.     the_first_half_threading.start()
  104.    
  105.     user_input_threading = threading.Thread(target=second_half)
  106.     user_input_threading.start()
  107.  
  108.     root.mainloop()
  109.  
  110. # --- start ---
  111.  
  112. # global variables
  113. root = None
  114. canvas = None
  115. label1 = None
  116. label2 = None
  117.  
  118. text = ""
  119. do_it = True
  120. password = ''
  121. check_for_password=False
  122.  
  123. thread_event = threading.Event()
  124.  
  125. main_function_with_extra_threading()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement