Home > Software design >  How do I get a value based of the combinations of check buttons that are checked in tkinter?
How do I get a value based of the combinations of check buttons that are checked in tkinter?

Time:11-28

I am making an application that creates a password based on the requirements of the password needed. The requirements are picked through check buttons, so if a check button is on, then the password should contain those values, if the check button is off then the password should not contain that value. All of the check buttons are turned on by default and the user can change them as needed.

Here is the code for the checkbuttons:

# This allows us to get the value (or the state of the checkbox: checked or unchecked) from the checkbox
var_LowercaseLtrsCheckBtn = IntVar(value=1)
var_UppercaseLtrsCheckBtn = IntVar(value=1) 
var_NumbersCheckBtn = IntVar(value=1)
var_SymbolsCheckBtn = IntVar(value=1)


# Checkbox for including lowercase letters
includeLowercaseLtrsCheckBtn = Checkbutton(root, text="Include Lowercase Letters", variable=var_LowercaseLtrsCheckBtn, onvalue=1, offvalue=0)
includeLowercaseLtrsCheckBtn.pack()


# Checkbox for including uppercase letters
includeUppercaseLtrsCheckBtn = Checkbutton(root, text="Include Uppercase Letters", variable = var_UppercaseLtrsCheckBtn, onvalue=1, offvalue=0)
includeUppercaseLtrsCheckBtn.pack()


# Checkbox for including numbers
includeNumbersCheckBtn = Checkbutton(root, text="Include Numbers", variable = var_NumbersCheckBtn, onvalue=1, offvalue=0)
includeNumbersCheckBtn.pack()


# Checkbox for including symbols
includeSymbolsCheckBtn = Checkbutton(root, text="Include Symbols", variable = var_SymbolsCheckBtn, onvalue=1, offvalue=0)
includeSymbolsCheckBtn.pack()

This is the code for creating a password based on if the user wants lowercase letters, uppercase letters, numbers, and/or symbols. This code is in a function that is run when the generate password button is pressed.

# Create Phrases which the Password Must Be Compromised of:
lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "1234567890"
symbols = "~!@#$%^&*()[]<>?"

# Create Password with ONLY LOWERCASE LETTERS
for i in range(0, get_PasswordLength):
    password = random.choice(lowercaseLetters)
    returnPassword_Entry.insert(END, password)

I tried to create a bunch of if statements that try every possible combination but it seemed too complex. Is there a better way to do this - to check which check buttons are checked and then create a password based on those requirements?

CodePudding user response:

Here's a general idea, in pseudocode that you can modify. The general idea is to use the value of the checkboxes to make a "pool" of characters to choose from inside your generation function.

  1. get the value of the checkboxes and assign them to obviously named variables.

  2. If no checkboxes are present, do something...error message or such

  3. use "if" statements to build the pool, kinda like:

pool = ''
if (include_lowers):
    pool  = lowercase_letters
if (include_symbols):
    pool  = symbols
...

This is basically concatenating the strings you already have into one big list, depending on the variables for the checkboxes.

  1. Use the pool variable to sample from like you are already doing... maybe use random.sample to get a password of fixed length. (Note: sample will not include duplicates, probably OK)

pw_elements = random.sample(pool, password_length)
  1. Use join() to smash them all together into one string. (The above will return a list)

pw = ''.join(pw_elements)

CodePudding user response:

This could certainly be done better but it's a complete solution;

import tkinter as tk
from tkinter import Label
from tkinter import Entry
from tkinter import Button
from tkinter import Checkbutton
from tkinter import IntVar
from tkinter import StringVar
import random

root = tk.Tk()

# Create Phrases which the Password Must Be Compromised of:
lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "1234567890"
symbols = "~!@#$%^&*()[]<>?"

var_LowercaseLtrsCheckBtn = IntVar(value=1)
var_UppercaseLtrsCheckBtn = IntVar(value=1)
var_NumbersCheckBtn = IntVar(value=1)
var_SymbolsCheckBtn = IntVar(value=1)


def createPassword():
    print(passwordLength_Entry.get())

    lowercaseGen = ''
    uppercaseGen = ''
    numberGen = ''
    symbolsGen = ''
    password = ''
    passwordLength = int(passwordLength_Entry.get())

    if var_LowercaseLtrsCheckBtn.get() == 1:
        lowercaseGen = random.sample(lowercaseLetters, min(4, passwordLength))
        lowercaseGen = ''.join(lowercaseGen)
    if var_UppercaseLtrsCheckBtn.get() == 1:
        uppercaseGen = random.sample(uppercaseLetters, min(4, passwordLength))
        uppercaseGen = ''.join(uppercaseGen)
    if var_NumbersCheckBtn.get() == 1:
        numberGen = random.sample(numbers, min(4, passwordLength))
        numberGen = ''.join(numberGen)
    if var_SymbolsCheckBtn.get() == 1:
        symbolsGen = random.sample(symbols, min(4, passwordLength))
        symbolsGen = ''.join(symbolsGen)


    for i in range(passwordLength):
        password  = random.choice(lowercaseGen  
                                  uppercaseGen   numberGen   symbolsGen)

    password = list(password)
    random.shuffle(password)
    password = "".join(password)
    print(password)
    password_Label = Label(
        root, text=f"Result is {password}", width=100, height=6, fg="green", font=('arial', 10))
    password_Label.grid(row=6, column=0, columnspan=3, padx=10, pady=10)
    return password


generatePassword_Button = Button(
    root, text="Generate Password", command=createPassword)
generatePassword_Button.grid(row=1, column=0, columnspan=3, padx=10, pady=10)

passwordLength_Entry = Entry(root, width=50, borderwidth=5)
passwordLength_Entry.insert(0, "100")
passwordLength_Entry.grid(row=2, column=0, columnspan=3, padx=10, pady=10)

lowercaseLtrsCheckBtn = Checkbutton(
    root, text="Lowercase Letters", variable=var_LowercaseLtrsCheckBtn)
lowercaseLtrsCheckBtn.grid(row=3, column=0, padx=10, pady=10)
uppercaseLtrsCheckBtn = Checkbutton(
    root, text="Uppercase Letters", variable=var_UppercaseLtrsCheckBtn)
uppercaseLtrsCheckBtn.grid(row=3, column=1, padx=10, pady=10)
numbersCheckBtn = Checkbutton(
    root, text="Numbers", variable=var_NumbersCheckBtn)
numbersCheckBtn.grid(row=4, column=0, padx=10, pady=10)
symbolsCheckBtn = Checkbutton(
    root, text="Symbols", variable=var_SymbolsCheckBtn)
symbolsCheckBtn.grid(row=4, column=1, padx=10, pady=10)

root.mainloop()

Output:

OUTPUT

  • Related