Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. '''
  2. A Python Exercise: Reading CSV File & Tkinter Implementation
  3. Including the subjects: Grid Layout, Tk Variables and Binding
  4. '''
  5. import Tkinter as tk
  6. import csv
  7.  
  8. CSV_FILE = "users.csv"
  9.  
  10. # Functions
  11. def popup(title, msg):
  12. '''Open popup window with title and msg'''
  13. w = tk.Toplevel(root)
  14. w.title(title)
  15. w.minsize(200, 200)
  16. tk.Label(w, text=msg).pack()
  17. tk.Button(w, text="Close", command=w.destroy).pack(pady=10)
  18. w.bind("<Return>", lambda f: w.destroy())
  19.  
  20. def read_from_file():
  21. '''Read csv file and return a list like: [[username, password, count]]'''
  22. try:
  23. with open(CSV_FILE, 'rb') as f:
  24. users = []
  25. reader = csv.reader(f)
  26. for row in reader:
  27. row[2] = int(row[2]) # Make the count an integer so it can increase later
  28. users.append(row)
  29. return users
  30. except IOError:
  31. popup("Error", "File not found!")
  32.  
  33. def write_to_file(users):
  34. '''Get a list of all users and write it to the csv file'''
  35. with open(CSV_FILE, 'wb') as f:
  36. writer = csv.writer(f)
  37. for row in users:
  38. row[2] = str(row[2])
  39. writer.writerows(users)
  40.  
  41. class App(tk.Frame):
  42. def __init__(self, master=None):
  43. tk.Frame.__init__(self, master)
  44. self.master = master
  45. self.pack(anchor="w")
  46.  
  47. # Labels
  48. tk.Label(self, text="Username:").grid(sticky="w")
  49. tk.Label(self, text="Password:").grid(row=1, sticky="e")
  50.  
  51. # Entries
  52. self.n_string = tk.StringVar()
  53. self.p_string = tk.StringVar()
  54. name = tk.Entry(self, textvariable=self.n_string, width=30)
  55. passw = tk.Entry(self, textvariable=self.p_string, show='*', width=20)
  56. name.grid(row=0, column=1, sticky="we")
  57. passw.grid(row=1, column=1, sticky="we")
  58.  
  59. # Check Button
  60. self.check_var = tk.BooleanVar()
  61. check = tk.Checkbutton(self, text="Count Login attempts", variable=self.check_var)
  62. check.grid(row=2, column=1, sticky="w")
  63.  
  64. # Login Button
  65. login = tk.Button(self, text="Login", command=self.login)
  66. login.grid(row=2, column=1, sticky="e")
  67.  
  68. # Binding
  69. self.master.bind("<Return>", self.login)
  70.  
  71. def login(self, event=None):
  72. '''Login attempt'''
  73. users = read_from_file()
  74. if not users:
  75. return
  76. for row in users:
  77. if row[0] == self.n_string.get() and row[1] == self.p_string.get():
  78. msg = "Welcome %s!" % row[0]
  79. popup("Welcome", msg)
  80. if self.check_var.get(): # Check if "Count Login attempts" button is checked
  81. row[2] += 1 # add +1 login count
  82. write_to_file(users)
  83. break
  84. elif row[0] == self.n_string.get():
  85. print "Password incorrect."
  86. break
  87. else: # If there isn't a match of the username
  88. print "User name incorrect!"
  89.  
  90.  
  91. # GUI settings
  92. root = tk.Tk()
  93. app = App(root)
  94. root.title("Login Form")
  95. root.minsize(200, 200)
  96.  
  97. # Initalize GUI
  98. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement