Python | Random password generator using Tkinter

| | | | | |

Let’s create a simple application that can randomly generate strong passwords using the Python module import random import pyperclip from tkinter import * from tkinter. ttk import *

Below is the implementation:

# Python program to generate random
# password using the Tkinter module

import random

import pyperclip

from tkinter import *

from tkinter.ttk import *


# Function for calculating the password

def low ():

entry.delete ( 0 , END)

# Get passowrd length

length = var1.get ()

lower = "abcdefghijklmnopqrstuvwxyz"

upper = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "

digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789! @ # $% ^ & Amp; * ()"

password = ""

# if the selected strength is low

if var.get () = = 1 :

for i in range ( 0 , leng th):

password = password + random.choice (lower)

return password

# if the selected strength is medium

elif var.get () = = 0 :

for i in range ( 0 , l ength):

password = password + random.choice (upper)

return password

# if the chosen force is strong

elif var.get () = = 3 :

for i in range ( 0 , length):

password = password + random.choice (digits)

return password

else :

print ( "Please choose an option" )


# Function for generating password

def generate ():

password1 = low ()

entry.insert ( 10 , password1)


# Function for copying the password to the clipboard

def copy1 ():

random_password = entry.get ()

pyperclip.copy (random_password)


# Main function


# create GUI window

< p> root = Tk ()

var = IntVar ()

var1 = IntVar ()


# The title of your GUI window

root.title ( "Random Password Generator" )


# create a shortcut and post to show
# password generated

Random_password = Label (root, text = " Password " )

Random_password.grid (row = 0 )

entry = Entry (root)

entry.grid (row = 0 , column = 1 )


# create a label for password length

c_label = Label (root, text = "Length" )

c_label.grid (row = 1 )


# Create buttons that will t copy
# the password to the clipboard and generate
# which will generate the password

copy_button = Button (root, text = "Copy" , command = copy1)

copy_button.grid (row = 0 , column = 2 )

generate_button = Button (root, text = "Generate" , command = generate)

generate_button.grid (row = 0 , column = 3 )


# Radio buttons for making decisions
# password strength
# Medium by default

radio_low = Radiobutton (root, text = "Low" , variable = var, value = 1 )

radio_low.grid (row = 1 , column = 2 , sticky = ’E’ )

radio_middle = Radiobutton (root, text = "Medium" , variable = var, value = 0 )

radio_middle.grid (row = 1 , column = 3 , sticky = ’E’ )

radio_strong = Radiobutton (root, text = "Strong" , variable = var, value = 3 )

radio_strong.grid (row = 1 , column = 4 , sticky = ’E’ )

combo = Combobox (root, textvariable = var1)


# Combo box for your password length

combo [ ’ values’ ] = ( 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ,

17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 ,

26 , 27 , 28 , 29 , 30 , 31 , 32 , "Length" )

combo.current ( 0 )

combo.bind ( ’& lt; & lt; ComboboxSelected & gt; & gt;’ )

combo.grid (column = 1 , row = 1 )


# launch GUI
root.mainloop ()

Output:

Python | Random password generator using Tkinter __del__: Questions

How can I make a time delay in Python?

5 answers

I would like to know how to put a time delay in a Python script.

2973

Answer #1

import time
time.sleep(5)   # Delays for 5 seconds. You can also use a float value.

Here is another example where something is run approximately once a minute:

import time
while True:
    print("This prints once a minute.")
    time.sleep(60) # Delay for 1 minute (60 seconds).

2973

Answer #2

You can use the sleep() function in the time module. It can take a float argument for sub-second resolution.

from time import sleep
sleep(0.1) # Time in seconds

How to delete a file or folder in Python?

5 answers

How do I delete a file or folder in Python?

2639

Answer #1


Path objects from the Python 3.4+ pathlib module also expose these instance methods:

Python | Random password generator using Tkinter __delete__: Questions

2639

Answer #2


Path objects from the Python 3.4+ pathlib module also expose these instance methods:

2639

Answer #3

Python syntax to delete a file

import os
os.remove("/tmp/<file_name>.txt")

Or

import os
os.unlink("/tmp/<file_name>.txt")

Or

pathlib Library for Python version >= 3.4

file_to_rem = pathlib.Path("/tmp/<file_name>.txt")
file_to_rem.unlink()

Path.unlink(missing_ok=False)

Unlink method used to remove the file or the symbolik link.

If missing_ok is false (the default), FileNotFoundError is raised if the path does not exist.
If missing_ok is true, FileNotFoundError exceptions will be ignored (same behavior as the POSIX rm -f command).
Changed in version 3.8: The missing_ok parameter was added.

Best practice

  1. First, check whether the file or folder exists or not then only delete that file. This can be achieved in two ways :
    a. os.path.isfile("/path/to/file")
    b. Use exception handling.

EXAMPLE for os.path.isfile

#!/usr/bin/python
import os
myfile="/tmp/foo.txt"

## If file exists, delete it ##
if os.path.isfile(myfile):
    os.remove(myfile)
else:    ## Show an error ##
    print("Error: %s file not found" % myfile)

Exception Handling

#!/usr/bin/python
import os

## Get input ##
myfile= raw_input("Enter file name to delete: ")

## Try to delete the file ##
try:
    os.remove(myfile)
except OSError as e:  ## if failed, report it back to the user ##
    print ("Error: %s - %s." % (e.filename, e.strerror))

RESPECTIVE OUTPUT

Enter file name to delete : demo.txt
Error: demo.txt - No such file or directory.

Enter file name to delete : rrr.txt
Error: rrr.txt - Operation not permitted.

Enter file name to delete : foo.txt

Python syntax to delete a folder

shutil.rmtree()

Example for shutil.rmtree()

#!/usr/bin/python
import os
import sys
import shutil

# Get directory name
mydir= raw_input("Enter directory name: ")

## Try to remove tree; if failed show an error using try...except on screen
try:
    shutil.rmtree(mydir)
except OSError as e:
    print ("Error: %s - %s." % (e.filename, e.strerror))

Is there a simple way to delete a list element by value?

5 answers

I want to remove a value from a list if it exists in the list (which it may not).

a = [1, 2, 3, 4]
b = a.index(6)

del a[b]
print(a)

The above case (in which it does not exist) shows the following error:

Traceback (most recent call last):
  File "D:zjm_codea.py", line 6, in <module>
    b = a.index(6)
ValueError: list.index(x): x not in list

So I have to do this:

a = [1, 2, 3, 4]

try:
    b = a.index(6)
    del a[b]
except:
    pass

print(a)

But is there not a simpler way to do this?

1055

Answer #1

To remove an element"s first occurrence in a list, simply use list.remove:

>>> a = ["a", "b", "c", "d"]
>>> a.remove("b")
>>> print(a)
["a", "c", "d"]

Mind that it does not remove all occurrences of your element. Use a list comprehension for that.

>>> a = [10, 20, 30, 40, 20, 30, 40, 20, 70, 20]
>>> a = [x for x in a if x != 20]
>>> print(a)
[10, 30, 40, 30, 40, 70]

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method