1

The label in this application does not display the password once the generate button is clicked. I think it might be from the "for" loop. Any help would be appreciated.


from tkinter import *
from tkinter import ttk
import random
window = Tk()
window.resizable(False , False)
window.title('Password Generator')
window.geometry('400x200')

length = IntVar()
lbl = StringVar()
var1 = StringVar()

Alphabet = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
showpass = ttk.Label(window , textvariable = lbl).pack()

def randalp():

    for i in range(length):
        random.choice(Alphabet)

ttk.Label(window , text = 'Password Lentgh:').pack()
numchoosen = ttk.Combobox(window, width = 12 , textvariable = length)
numchoosen['values'] = (5,6,7,8,9,10)
numchoosen.pack()
numchoosen.current(2)
numchoosen.config(state = 'readonly')
rad1 = ttk.Checkbutton(window , text = 'Alphabet' , variable = var1).pack()

def btnclick():
    get1 = var1.get()
    if rad1 == '1':
            lbl.set(randalp)
            print(lbl)

btn = ttk.Button(window , text = 'Generate' , command = btnclick).pack()
window.mainloop()
tamerjar
  • 220
  • 2
  • 12
  • Please do not use len to declare a variable as that is a reserved keyword in python. – tamerjar Apr 25 '21 at 08:20
  • the for loop seems to do absolutely nothing, You are not doing anything with the `random.choice` I mean it returns something but You don't use that value. Maybe You meant `random.shuffle()`? in that case no need for `for loop` – Matiiss Apr 25 '21 at 08:31

1 Answers1

2

Does this fix it for You:

First You have to change Your randalp() function a bit:

def randalp():
    string = list(Alphabet)
    random.shuffle(string)
    return string[:5]

I used shuffle because it just shuffles a list in a random order, so taking the first 5 items from that list would be random.

Also changed the btnclick() function:

def btnclick():
    get1 = var1.get()
    if get1 == '1':
        lbl.set(randalp())

so now it uses the returned value of randalp()

Also You should fix a few formatting issues with Your code: suggestion: follow PEP 8 because it makes the code easier to read and don't use built-in function names (like len) as variable names; it is quite bad practice.

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • tnx for your help now how to change 5 in `return string[:5]` i need to use combobox here that change with IntVar() but doesnt work and then use variable instead of 5 and finally how to use numbers next to the alphabet in function because my program has 2 check button more numbers and symobls next to the alphabet `def randan(): string = list(Alphabet and Numbers) random.shuffle(string) return string[:5]` i cant use this sry for my bad english –  Apr 25 '21 at 22:08
  • what exactly doesn't work with changing `5` using `IntVar()` (You could ask another question and give a link here)? also `and` and `or` are logic gates, which means they don't combine anything, You could use `string = list(Alphabet) + list(Numbers)` – Matiiss Apr 26 '21 at 12:22
  • TNX Bro for your help –  Apr 27 '21 at 09:42