Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
from kivy.app import App
from kivy.uix.widget import Widget
import random

class PasswordGenerator(Widget):
    pass


class PasswordGenerator(App):
    def build(self):
        return PasswordGenerator()


if __name__ == '__main__':
    PasswordGenerator().run()

print ('Podaj liczbę znaków, które chcesz mieć w haśle: ')
   
password_length = int(input())
    
print('Twoje hasło: ')

chars = 'abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*'

password = ''
for x in range(password_length):
    password += random.choice(chars)

print(password)

class PasswordGenerator(Widget):
    pass


class PasswordGenerator(App):
    def build(self):
        return PasswordGenerator()


if __name__ == '__main__':
    PasswordGenerator().run()


What I have tried:

I keep getting an error in
Python
if __name__ == '__main__':
    PasswordGenerator().run()

The error says:
Exception: Invalid instance in App.root

What is wrong?
Posted
Updated 28-Jul-23 2:20am
v2

1 solution

You are defining two classes with the same name, 'PasswordGenerator' and the second class definition is overwriting the first one. Combine the two class definitions into one and remove the duplicate class -

Python
from kivy.app import App
from kivy.uix.widget import Widget
import random

class PasswordGenerator(Widget):
    def generate_password(self, password_length):
        chars = 'abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*'
        password = ''
        for x in range(password_length):
            password += random.choice(chars)
        return password

class PasswordGeneratorApp(App):
    def build(self):
        return PasswordGenerator()

if __name__ == '__main__':
    print('Podaj liczbę znaków, które chcesz mieć w haśle: ')
    password_length = int(input())
    print('Twoje hasło: ')
    generator_app = PasswordGeneratorApp()
    password = generator_app.root.generate_password(password_length)
    print(password)
    generator_app.run()


I renamed the one class that extends 'App' to 'PasswordGeneratorApp' to avoid the name conflict, should run now without the error.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900