0

So I have been working on my coursework and I am trying to encrypt a password. However, the code runs as soon as I start to initialise the program and won't run when I press the specified button. This is what I have so far:

import tkinter as tk
from cryptography.fernet import Fernet

#validate function
def validateStylist(window):
    password = passwordEntry.get().encode("utf-8")
    cipher_text = cipher_suite.encrypt(password)
    plain_text = cipher_suite.decrypt(cipher_text)
    print(plain_text)

key = Fernet.generate_key()
cipher_suite = Fernet(key)

MEDIUM_FONT = ("Berlin Sans FB", 12)
LARGE_FONT = ("Berlin Sans FB", 16)

window = tk.Tk()

titleLabel = tk.Label(window, text="Register Stylist", font=LARGE_FONT, bg="#FFC0CB")
titleLabel.grid(columnspan = 4)

#Password
passwordLabel = tk.Label(window, text="Password:", font=MEDIUM_FONT, bg="#FFC0CB")
passwordLabel.grid(row=1,column=3)
passwordVar = tk.StringVar(window)
passwordEntry = tk.Entry(window, textvariable=passwordVar)
passwordEntry.grid(row=2,column=3)

finishButton = tk.Button(window, text="Finish",
                          command=validateStylist(window))
finishButton.grid(row=4, column=3, sticky="ew")


window.mainloop()
C Farrell
  • 25
  • 6

1 Answers1

1

Using lambda in the following way should fix your problem; It will ensure that the button only runs the code when pressed, rather than beforehand (when the program runs).

command=lambda: validateStylist(window)

So, for clarity, your new line would be:

finishButton = tk.Button(window, text="Finish",
                      command=lambda: validateStylist(window))
Chris
  • 351
  • 2
  • 4
  • 13