Answer the question
In order to leave comments, you need to log in
There is a GUI template for generating passwords with the ability to choose the length and number of passwords. How to bind generate function to widgets?
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.config import Config
import random
Config.set('graphics', 'resizable', '1')
Config.set('graphics', 'width', '350')
Config.set('graphics', 'height', '150')
passnumber = input(int(""))
passlength = input(int(""))
chars = '+-/*!&$#[email protected]<>%^:;""_~()abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
class PassGenApp(App):
def generate(self):
global passnumber
global passlength
for n in range(passnumber):
password = ''
for i in range(passlength):
password += random.choice(chars)
print(password)
def build(self, **kwargs):
bl = BoxLayout(orientation='vertical')
gl = GridLayout(cols=2)
bl.add_widget(Label(text=''))
gl.add_widget(Label(text='PassNum:'))
gl.add_widget(TextInput())
gl.add_widget(Label(text='PassLength:'))
gl.add_widget(TextInput())
gl.add_widget(Button(text='Generate'))
gl.add_widget(bl)
return gl
if __name__ =='__main__':
PassGenApp().run()
Answer the question
In order to leave comments, you need to log in
Of course, I have never used kivy, but the question is quite easy)
But, I can suggest you change the code something like this:
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.config import Config
from random import choice
Config.set('graphics', 'resizable', '1')
Config.set('graphics', 'width', '350')
Config.set('graphics', 'height', '150')
class PassGenApp(App):
txt1 = TextInput()
txt2 = TextInput()
chars = '+-/*!&$#[email protected]<>%^:;""_~()abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
def generate(self):
print(["".join([choice(self.chars) for i in range(int(self.txt2.text))]) for passcount in range(int(self.txt1.text))])
def build(self, **kwargs):
bl = BoxLayout(orientation='vertical')
gl = GridLayout(cols=2)
bl.add_widget(Label(text=''))
gl.add_widget(Label(text='PassNum:'))
gl.add_widget(self.txt1)
gl.add_widget(Label(text='PassLength:'))
gl.add_widget(self.txt2)
gl.add_widget(Button(text='Generate'))
gl.add_widget(bl)
return gl
if __name__ =='__main__':
PassGenApp().run()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question