Python Forum
[Tkinter] password with Entry widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] password with Entry widget
#9
Creating GUI code is easier if you use classes.
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()
Reply


Messages In This Thread
password with Entry widget - by TAREKYANGUI - Sep-16-2020, 03:16 AM
RE: password with Entry widget - by bowlofred - Sep-16-2020, 05:34 AM
RE: password with Entry widget - by TAREKYANGUI - Sep-16-2020, 08:37 PM
RE: password with Entry widget - by micseydel - Sep-17-2020, 06:01 PM
RE: password with Entry widget - by deanhystad - Sep-17-2020, 06:48 PM
RE: password with Entry widget - by TAREKYANGUI - Sep-22-2020, 01:56 PM
RE: password with Entry widget - by SnowwyWolf - Sep-23-2020, 08:53 PM
RE: password with Entry widget - by Yoriz - Sep-23-2020, 09:40 PM
RE: password with Entry widget - by TAREKYANGUI - Sep-24-2020, 05:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 914 Mar-06-2024, 08:42 PM
Last Post: russellm44
  [Tkinter] entry widget DPaul 5 1,623 Jul-28-2023, 02:31 PM
Last Post: deanhystad
  Tkinter Exit Code based on Entry Widget Nu2Python 6 3,112 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,322 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  Entry Widget issue PA3040 16 7,040 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,521 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to retreive the grid location of an Entry widget kenwatts275 7 4,755 Apr-24-2020, 11:39 PM
Last Post: Larz60+
  POPUP on widget Entry taratata2020 4 3,836 Mar-10-2020, 05:04 PM
Last Post: taratata2020
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,513 Jan-23-2020, 09:00 PM
Last Post: HBH
  The coordinates of the Entry widget (canvas) when moving berckut72 8 6,101 Jan-07-2020, 09:26 AM
Last Post: berckut72

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020