Home > Enterprise >  AttributeError: 'str' object has no attribute '_root' in python3 on windows 10
AttributeError: 'str' object has no attribute '_root' in python3 on windows 10

Time:02-27

My code:

from tkinter import *
from tkinter.ttk import Combobox
import random

screen = Tk()
screen.title("Password Generator")
screen.geometry('600x400')
screen.configure(background ="gray")

def Password_Gen():
    global sc1
    sc1.set("")
    passw=""
    length=int(c1.get())
    lowercase='abcdefghijklmnopqrstuvwxyz'
    uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ' lowercase
    mixs='0123456789' lowercase uppercase '@#$%&*'

    if c2.get()=='Low Strength':
        for i in range(0,length):
            passw=passw random.choice(lowercase)
        sc1.set(passw)
    elif c2.get()=='Medium Strength':
            for i in range(0,length):
                passw=passw random.choice(uppercase)
            sc1.set(passw)
    elif c2.get()=='High Strength':
            for i in range(0,length):
                passw=passw random.choice(mixs)
            sc1.set(passw)

sc1=StringVar('')
t1=Label(screen,text='Password Generator',font=('Arial',25),fg='green',background ="gray")
t1.place(x=60,y=0)
t2=Label(screen,text='Password:',font=('Arial',14),background ="gray")
t2.place(x=145,y=90)

il=Entry(screen,font=('Arial',14),textvariable=sc1)
il.place(x=270,y=90)
t3=Label(screen,text='Length: ',font=('Arial',14),background ="gray")
t3.place(x=145,y=120)

t4=Label(screen,text='Strength:',font=('Arial',14),background ="gray")
t4.place(x=145,y=155)

c1=Entry(screen,font=('Arial',14),width=10)
c1.place(x=230,y=120)

c2=Combobox(screen,font=('Arial',14),width=15)
c2['values']=('Low Strength','Medium Strength','High Strength')
c2.current(1)
c2.place(x=237,y=155)

b=Button(screen,text='Generate!',font=('Arial',14),fg='green',background ="gray",command=gen)
b.place(x=230,y=195)

screen.mainloop()

For some reason I'm keep getting those errors:

Traceback (most recent call last):
  File "C:\Users\z\3D Objects\birthday\passwordgeneratorgui.py", line 32, in <module>
    sc1=StringVar('')
  File "C:\Users\z\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 540, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Users\z\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 372, in __init__
    self._root = master._root()
AttributeError: 'str' object has no attribute '_root'

How can I fix those errors?

CodePudding user response:

This is because the first argument to StringVar should be the "container". You are passing in an empty string instead of that. Replace the line sc1=StringVar('') with sc1=StringVar(). You can also read a guide to StringVars like this one: https://www.pythontutorial.net/tkinter/tkinter-stringvar/

Another mistake in your code is in this line:

b=Button(screen,text='Generate!',font=('Arial',14),fg='green',background ="gray",command=gen)

The problem is that you are passing in a function gen, which doesn't exist. I guess that you want to call the function Password_Gen with this button. So, replace it with this line:

b=Button(screen,text='Generate!',font=('Arial',14),fg='green',background ="gray",command=Password_Gen)

EDIT:

I have also noticed a minor problem with your password generator.

lowercase='abcdefghijklmnopqrstuvwxyz'
uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ' lowercase
mixs='0123456789' lowercase uppercase '@#$%&*'

In the mixs variable, there will be lowercase letters twice, I don't think you want this. I think what you want is this (because lowercase letters are already included in your uppercase variable):

mixs='0123456789' uppercase '@#$%&*'
  • Related