S
S
Simple Ian2020-08-12 10:21:21
Python
Simple Ian, 2020-08-12 10:21:21

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

1 answer(s)
T
Ternick, 2020-08-13
@Ternick

Of course, I have never used kivy, but the question is quite easy)
But, I can suggest you change the code something like this:

THE CODE

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()


I think somehow it should be like this, although this is not an ideal option and from the side of correct writing of the code, it is better to close your eyes and not look, but I think it will work.
If you are satisfied with the answer, mark the answer as solved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question