Login System; i have a this error.How to get text in txt1 and how to change via button ?
File "/home/hypermesh/Desktop/main.py", line 11, in messageShow if self.txt1.text == "stock": AttributeError: 'Button' object has no attribute 'txt1'
#-*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
def messageShow(self):
if self.txt1.text == "stock":
pop=Popup(text="yes")
else:
pop=Popup(text="error")
class SimpleKivy(App):
def build(self):
grid=GridLayout(rows=3, cols=2)
lbl1=Label(text="ID :",italic=True, bold=True)
lbl2=Label(text="Password :",italic=True, bold=True)
txt1=TextInput(multiline=False, font_size=50)
txt2=TextInput(multiline=False, password=True)
btn1=Button(text="Exit",italic=True)
btn2=Button(text="OK",italic=True)
btn2.bind(on_press=messageShow)
grid.add_widget(lbl1)
grid.add_widget(txt1)
grid.add_widget(lbl2)
grid.add_widget(txt2)
grid.add_widget(btn1)
grid.add_widget(btn2)
return grid
if __name__ == "__main__":
SimpleKivy().run()
you did it right ... but you must save a reference to anthing you want to access later (usually attaching it to self)
def __init__(...):
...
self.txt1=TextInput(multiline=False, font_size=50)
...
then your other function should work fine (except the method should be part of the class..)
class SimpleKivy(App):
def messageShow(self,evt):
if self.txt1.text == "stock":
pop=Popup(text="yes")
else:
pop=Popup(text="error")
def build(self):
grid=GridLayout(rows=3, cols=2)
lbl1=Label(text="ID :",italic=True, bold=True)
another alternative is to use lambdas to call it
def messageShow(message):
print "GOT MESSAGE:",message
class SimpleKivy(App):
def __init__(self,...):
txt1 = TextInput(...)
...
btn.bind(on_press=lambda *a:messageShow(txt1.text))
in this case txt1 is in the variable scope and able to pass its string to messageShow