What this code in Python does is, it will generate a password of length the user wants using his details which would be more relatable to him.
In this tutorial, we will be making a GUI window using Tkinter GUI in python. We will go through the basic libraries and concepts and codes in Tkinter.
What this code does is, it will generate a password of length whatever the user wants using his data which would be more relatable to him.
GUI stands for Graphical User Interface, which helps us the users to interact with the machine in a more fun way or through an interface. Tkinter is the method that provides a toolkit for GUI.
As we are starting with the code some steps are to be remembered.
Importing the module
From tkinter import* import os import random
from tkinter import * import os import random screen=Tk() screen.title("Password generator") screen.geometry("400x400") screen.configure(bg='orange')
It includes all the names of Tkinter in the code. os module triggers when Python starts. It assigns its path to the os specific module attribute. The random module is for generating random values or pseudo values.
Initializing the variable
screen=Tk()
To create the main window, Tkinter offers a method TK().
Creation of window
You can add title and
window geometry to add details to your window.
Adding widgets to it
In this code the widgets used are
Button: this is used to place buttons in Tkinter
Label: A single line widget that displays a box where text can be inserted
Entry: This is the input widget, any single line input is accepted here.
Text: Inserts text in Tkinter
Closing through a variable in mainloop()
This is the final closing statement in your code. It tells python to run the code in loops until the window is closed.
Getting started with the code
After creating a window, using below code
We need to place widgets for which I would use the label as
Label(screen, text="Please enter details below", bg="gray",font=("Arial", 19, "bold")).pack() fname_lable = Label(screen, text="First name ",bg='light gray',font=("Times New Roman", 15, "bold"))
here, we can insert text to our label added with colour,edit font,and use pack function to use the available space efficiently in window.
Also, the text widget is used here.
Button widget is used as
exit_button = Button(screen, text="Exit",font=("Times New Roman", 15, "italic"), command=screen.destroy) exit_button.pack(padx=10,pady=10)
Destroy command destroys the window. When the button is clicked, the application terminates.
Defining of a password generator function
Accessing entries from the user using the .get() function and using them to generate a password is done in the code.
Here the random function is used to generate a password with for() loop in it. Also, it is necessary to mention the type of input when accessing it, so we have written int and str before using the input function.
def passwordgen(): password_field.delete(0, END) length = int(cinput.get()) fname=str(fname_entry.get()) lname=str(lname_entry.get()) dob=str(dob_entry.get()) password_chars =fname+lname+dob password = "".join([random.choice(password_chars) for k in range(length)]) password_field.insert(0, password)
Complete Code:
#import modules from tkinter import * import os import random # Designing window for registration screen=Tk() screen.title("Password generator") screen.geometry("400x400") screen.configure(bg='orange') global fname global fname global dob username = StringVar() password = StringVar() def passwordgen(): password_field.delete(0, END) length = int(cinput.get()) fname=str(fname_entry.get()) lname=str(lname_entry.get()) dob=str(dob_entry.get()) password_chars =fname+lname+dob password = "".join([random.choice(password_chars) for k in range(length)]) password_field.insert(0, password) Label(screen, text="Please enter details below", bg="gray",font=("Arial", 19, "bold")).pack() fname_lable = Label(screen, text="First name ",bg='light gray',font=("Times New Roman", 15, "bold")) fname_lable.pack(pady=10) fname_entry = Entry(screen,font=("Times New Roman", 15, "bold")) fname_entry.pack() fname_entry.focus() lname_lable = Label(screen, text="Last name ",bg='light gray',font=("Times New Roman", 15, "bold")) lname_lable.pack() lname_entry = Entry(screen,font=("Times New Roman", 15, "bold")) lname_entry.pack() lname_entry.focus() dob_lable = Label(screen, text="Date of birth ",bg='light gray',font=("Times New Roman", 15, "bold")) dob_lable.pack() dob_entry = Entry(screen,font=("Times New Roman", 15, "bold")) dob_entry.pack() dob_entry.focus() lenght_lable = Label(screen, text="lenght of password ",bg='light gray',font=("Times New Roman", 15, "bold")) lenght_lable.pack() cinput = Entry(screen,font=("Times New Roman", 15, "bold")) cinput.pack() cinput.focus() b1 = Button(screen, text = "get password",bg='gray',font=("Times New Roman", 10, "italic") ,command = passwordgen) b1.pack(padx=10,pady=10) sp_lable = Label(screen, text="Suggested Password ",bg='light gray',font=("Times New Roman", 15, "bold")) sp_lable.pack() password_field = Entry(font=("Arial", 15, "bold")) password_field.pack() exit_button = Button(screen, text="Exit",font=("Times New Roman", 15, "italic"), command=screen.destroy) exit_button.pack(padx=10,pady=10) screen.mainloop()
Output:
Submitted by Aanchal Kaushal Sharma (AanchalShar)
Download packets of source code on Coders Packet
Comments