TypeError: 'IntVar' object cannot be interpreted as an integer

Question:

How to fix ‘IntVar’ object cannot be interpreted as an integer? I’m making a GUI password generator based on my old console generator. After pressing "Generate" button, i getting that error.

import random
import string
from tkinter import *
from tkinter import ttk

tkWindow = Tk()  
tkWindow.geometry('250x150')  
tkWindow.title('Password Generator')

characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")

def passgen():

    random.shuffle(characters)

    password = []
    for i in range(length):
        password.append(random.choice(characters))

    random.shuffle(password)

    print("".join(password))

lengthLabel = Label(tkWindow, text="Password length:").grid(row=0, column=0)
length = IntVar()
lengthEntry = Entry(tkWindow, textvariable=length).grid(row=0, column=1)

GenButton = Button(tkWindow, text="Generate", command=passgen).grid(row=1, column=0) 

tkWindow.mainloop()
Asked By: Galuxosi

||

Answers:

Use length.get() in for loop present in passgen() method instead of length only. Like this:

    for i in range(length.get()):
        password.append(random.choice(characters))
Answered By: Mujtaba Mateen
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.