Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import tkinter as tk
- import threading
- #
- # PEP 8 - Style Guide for Python Code: https://www.python.org/dev/peps/pep-0008/
- #
- # PEP8: two empty lines between functions
- # PEP8: lower_case_names for functions
- # PEP8: more readable variables
- # PEP8: all "global" in separated lines at the beginning of function, and empty line at the end
- # PEP8: space after comma
- # PEP8: spaces around = (but not in function's arguments)
- def print_string(item, string):
- if item:
- for char in string:
- idx = canvas.index(item, tk.END)
- canvas.insert(item, idx, char)
- canvas.update()
- time.sleep(0.01)
- def key_event(label, event):
- global text
- global do_it
- global checkForPassword
- global password
- string = 'abcdefghijklmnopqrstuvwxyz'
- text = canvas.itemcget(label, "text")
- if do_it is False: # PEP8: for `False`/`True` use `is` instead of `==`/ `!=`
- thread_event.set()
- print('something')
- return True
- if event.keysym == "BackSpace":
- canvas.itemconfig(label1, text=text[:-1])
- elif event.keysym == 'space':
- canvas.itemconfig(label1, text=text+' ' )
- elif event.keysym == "Return":
- thread_event.set()
- password = text
- print('password is:', password)
- do_it = False
- check_for_password=True
- print('checkForPassword =', check_for_password)
- print(bool(event))
- elif event.char in string:
- # Character is an ASCII letter
- canvas.itemconfig(label1, text=text+event.char)
- def first_half():
- global canvas
- global label1
- global label2
- global entry
- # all variables
- prompt1 = """--------------------------------
- WELCOME TO GLOBAL DEFENSE SYSTEM
- --------------------------------
- PLEASE ENTER THE PASSWORD TO ACTIVATE EMERGENCY SHIELD PROCEDURE:"""
- canvas = tk.Canvas(root, width=700, height=400, bg='black')
- canvas.pack()
- canvas_txt = canvas.create_text(1, 0, fill= 'white', anchor='nw', font=('Courier New', 11))
- canvas.update()
- label1 = canvas.create_text(1, 68, text=text, fill='white', anchor='nw', font=('Courier New', 12))
- label2 = canvas.create_text(1, 72, text='', fill='white', anchor='nw', font=('Courier New', 11))
- print_string(canvas_txt, prompt1)
- root.bind('<Key>', lambda e: key_event(label1, e))
- root.update()
- def second_half():
- thread_event.wait()
- if password == 'united states':
- canvas.itemconfig(label2, text='CORRECT PASSWORD')
- else:
- canvas.itemconfig(label2, text='INCORRECT PASSWORD: ' + password)
- root.update()
- def main_function_with_extra_threading():
- global root
- root = tk.Tk()
- the_first_half_threading = threading.Thread(target=first_half)
- the_first_half_threading.start()
- user_input_threading = threading.Thread(target=second_half)
- user_input_threading.start()
- root.mainloop()
- # --- start ---
- # global variables
- root = None
- canvas = None
- label1 = None
- label2 = None
- text = ""
- do_it = True
- password = ''
- check_for_password=False
- thread_event = threading.Event()
- main_function_with_extra_threading()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement