Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- #import Tkinter as tk # Python 2.7
- import time
- # 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 first_half():
- global canvas
- global label
- 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()
- print_string(canvas_txt, prompt1)
- label = canvas.create_text(1, 72, text='', fill='white', anchor='nw', font=('Courier New', 11))
- entry = tk.Entry(root, width=32, relief='flat', font=('Courier New', 11),
- bg='grey', fg='white', highlightthickness=0, borderwidth=0, insertborderwidth=0)
- entry.bind('<Return>', second_half) # run second_half when "Enter" is pressed
- entry.focus() # it puts cursor in entry
- canvas_entry = canvas.create_window(1, 100, anchor='nw', window=entry)
- #button = tk.Button(root, text='OK', command=second_half,
- # relief='flat', font=('Courier New', 11),
- # bg='grey', fg='white', highlightthickness=0, borderwidth=0)
- #canvas_button = canvas.create_window(1, 130, anchor='nw', window=button)
- def second_half(event=None): # event=None to run it with Button(command=second_half)
- global password
- if event:
- password = event.widget.get()
- #event.widget.delete(0, 'end')
- #event.widget.insert(0, "a default value")
- else:
- password = entry.get()
- #entry.delete(0, 'end')
- #entry.insert(0, "a default value")
- if password == 'united states':
- canvas.itemconfig(label, text='CORRECT PASSWORD')
- else:
- canvas.itemconfig(label, text='INCORRECT PASSWORD')
- def main():
- global root
- root = tk.Tk()
- first_half()
- root.mainloop()
- # --- start ---
- # global variables
- root = None
- canvas = None
- label = None
- entry = None
- password = ''
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement