Advertisement
furas

Python - tkinter, canvas, entry

May 20th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. import tkinter as tk
  2. #import Tkinter as tk # Python 2.7
  3. import time
  4.  
  5. # PEP8: two empty lines between functions
  6. # PEP8: lower_case_names for functions
  7. # PEP8: more readable variables
  8. # PEP8: all "global" in separated lines at the beginning of function, and empty line at the end
  9. # PEP8: space after comma
  10. # PEP8: spaces around = (but not in function's arguments)
  11.  
  12.  
  13. def print_string(item, string):
  14.     if item:
  15.         for char in string:
  16.             idx = canvas.index(item, tk.END)
  17.             canvas.insert(item, idx, char)
  18.             canvas.update()
  19.             time.sleep(0.01)
  20.  
  21.  
  22. def first_half():
  23.     global canvas
  24.     global label
  25.     global entry
  26.    
  27.     # all variables
  28.     prompt1 = """--------------------------------
  29. WELCOME TO GLOBAL DEFENSE SYSTEM
  30. --------------------------------
  31. PLEASE ENTER THE PASSWORD TO ACTIVATE EMERGENCY SHIELD PROCEDURE:"""
  32.  
  33.     canvas = tk.Canvas(root, width=700, height=400, bg='black')
  34.     canvas.pack()
  35.  
  36.     canvas_txt = canvas.create_text(1, 0, fill= 'white', anchor='nw', font=('Courier New', 11))
  37.     #canvas.update()
  38.  
  39.     print_string(canvas_txt, prompt1)
  40.  
  41.     label = canvas.create_text(1, 72, text='', fill='white', anchor='nw', font=('Courier New', 11))
  42.  
  43.     entry = tk.Entry(root, width=32, relief='flat', font=('Courier New', 11),
  44.                      bg='grey', fg='white', highlightthickness=0, borderwidth=0, insertborderwidth=0)
  45.     entry.bind('<Return>', second_half) # run second_half when "Enter" is pressed
  46.     entry.focus() # it puts cursor in entry
  47.    
  48.     canvas_entry = canvas.create_window(1, 100, anchor='nw', window=entry)
  49.  
  50.     #button = tk.Button(root, text='OK', command=second_half,
  51.     #                    relief='flat', font=('Courier New', 11),
  52.     #                    bg='grey', fg='white', highlightthickness=0, borderwidth=0)
  53.                        
  54.     #canvas_button = canvas.create_window(1, 130, anchor='nw', window=button)
  55.  
  56. def second_half(event=None): # event=None to run it with Button(command=second_half)
  57.     global password
  58.    
  59.     if event:
  60.         password = event.widget.get()
  61.         #event.widget.delete(0, 'end')
  62.         #event.widget.insert(0, "a default value")
  63.     else:
  64.         password = entry.get()
  65.         #entry.delete(0, 'end')
  66.         #entry.insert(0, "a default value")
  67.    
  68.     if password == 'united states':
  69.         canvas.itemconfig(label, text='CORRECT PASSWORD')
  70.     else:
  71.         canvas.itemconfig(label, text='INCORRECT PASSWORD')
  72.  
  73. def main():
  74.     global root
  75.    
  76.     root = tk.Tk()
  77.    
  78.     first_half()
  79.  
  80.     root.mainloop()
  81.  
  82. # --- start ---
  83.  
  84. # global variables
  85. root = None
  86. canvas = None
  87. label = None
  88. entry = None
  89.  
  90. password = ''
  91.  
  92. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement