0

How can I use a lentgh variable in [:]?

I want to use a variable because I have a Combobox in tkinter. I get this error

TypeError: slice indices must be integers or None or have an index method

from tkinter import *
from tkinter import ttk
import random

window = Tk()
window.resizable(False , False)
window.title('Password Generator')
window.geometry('400x200')

lentgh = IntVar()
lbl = StringVar()
var1 = StringVar()

Alphabet = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'

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

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

ttk.Label(window , text = 'Password Lentgh:').pack()
numchoosen = ttk.Combobox(window, width = 12 , textvariable = lentgh)
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 get1 == '1':
                lbl.set(randalp())

btn = ttk.Button(window , text = 'Generate' , command = btnclick).pack()

window.mainloop()

1 Answers1

0

Dynamically slice a string using a variable

The above Post has some good tips on dynamically slicing strings with a variable.

if i understand what your trying to do correctly, your trying to take input for the length and randomly generate a password based on that length?

Dean
  • 11
  • 4
  • This doesn't help because `lentgh` is a `` not a normal python `int` – TheLizzard Apr 27 '21 at 09:19
  • have you tried converting the tkinter input ? numchoosen = ttk.Combobox(window, width = 12 , textvariable = int(lentgh)) – Dean Apr 27 '21 at 09:28
  • First of all I am not OP second of all look at how `tkinter.IntVar` works before you try to answer question that include it. You have the wrong idea of how `tkinter.IntVar` works – TheLizzard Apr 27 '21 at 09:35
  • Sorry man my bad, just came across it and slapped together and answer without thinking, length.get() instead of the classic int() my bad. – Dean Apr 27 '21 at 10:20