G
G
golub4ik2018-06-07 10:16:31
Python
golub4ik, 2018-06-07 10:16:31

Python + Kivy: TextInput update, how to implement?

I'm making a password manager for my personal use and decided to add a random password generator, when you click on the Generate Password button , the TextInput with the password should automatically update and accept the generated password. Code: (main info commented out)

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout

from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.config import Config

import string
from random import *

Config.set("graphics", "resizable", "0")
Config.set("graphics", "width", "700")
Config.set("graphics", "height", "800")

print("--------------")

class PasswordManagerApp(App):

  def open(self, args):
    pass

  def generate(self, args):
    self.password = "".join(choice(self.password_string) for x in range(randint(8, 12))) #генерация пароля
    print(self.password)

  def add(self, args):
    pass	


  def build(self):

    self.password_string = string.ascii_letters + string.digits

    self.resource = ""
    self.login = ""
    self.password = ""

    root = BoxLayout(orientation='vertical', padding = 5)

    top = GridLayout(cols = 6, padding = 5, size_hint = [1, .1])

    top.add_widget(Label(text='Ресурс'))
    top.add_widget(TextInput(text=''))
    top.add_widget(Label(text='Логин'))
    top.add_widget(TextInput(text=''))
    top.add_widget(Label(text='Пароль'))
    top.add_widget(TextInput(text=self.password)) #должно автоматически обновляться при нажатии на кнопку сгенерировать пароль

    root.add_widget(top)

    top_func = GridLayout(cols = 6, padding = 5, size_hint = [1, .1])

    top_func.add_widget(Button(text = 'Открыть файл', on_press = self.open))
    top_func.add_widget(Button(text = 'Сгенерировать пароль', on_press = self.generate)) #вот сама кнопка
    top_func.add_widget(Button(text = 'Добавить аккаунт', on_press = self.add))

    root.add_widget(top_func)


    output = TextInput(size_hint = [1, .8])
    root.add_widget(output)

    return root


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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
golub4ik, 2018-06-07
@golub4ik

Solution:
1. Assign a variable to the widget

self.test = TextInput(text='')
    top.add_widget(self.test)

2. register in the function:
Eh, I suffered

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question