Sep-23-2020, 09:40 PM
Creating GUI code is easier if you use classes.
Here is an example of your code turned into a class.
Here is an example of your code turned into a class.
import tkinter as tk class Main: def __init__(self, root): self.root = root self.rightpassword = "tarek" self.nb_attempt = 0 self.attempt_limit = 3 self.attempt_authorized = True root.title("Access Application") root.geometry("1400x480") # root.configure(bg = "#eaeaea") # Add Title1 lblTitle1 = tk.Label(root, text="Gestion des accès", font=( "Arial", 21), bg="darkblue", fg="white") lblTitle1.place(x=50, y=0, width=300) # password area lbpassword = tk.Label(root, text="Password :", font=( "Arial", 21), bg="darkblue", fg="white") lbpassword.place(x=450, y=200, width=300) entrypassword = tk.Entry(root, font=("Arial", 21)) entrypassword.bind("<Return>", self.entrypassword_return_event) entrypassword.place(x=780, y=200, width=300) self.entrypassword = entrypassword lbcomment = tk.Label(root, text="Comment :", font=( "Arial", 21), bg="black", fg="white") lbcomment.place(x=180, y=250, width=250) entrycomment = tk.Entry(root, font=("Arial", 21), bg="white", fg="red") entrycomment.place(x=450, y=250, width=800) self.entrycomment = entrycomment def entrypassword_return_event(self, event): word = self.entrypassword.get() if word != self.rightpassword: self.authorization_attempt() else: self.update_entry_comment("Congratulation Access authorized") self.authorized_code() def authorization_attempt(self): self.nb_attempt += 1 if self.nb_attempt < self.attempt_limit: self.attempt_authorized = True self.update_entry_comment(( "Try again, remaining attempts : " f"{self.attempt_limit-self.nb_attempt}")) else: self.attempt_authorized = False self.update_entry_comment("Access denied, out of trys") self.root.after(3000, self.root.quit) def update_entry_comment(self, comment): self.entrycomment.delete(0, tk.END) self.entrycomment.insert(0, comment) def authorized_code(self): print('add authorized code here') root = tk.Tk() Main(root) root.mainloop()