KIVY - 文本输入错误.kv

时间:2014-11-03 20:25:44

标签: python python-2.7 kivy

这是我的代码;

Main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest
from kivy.uix.listview import ListItemButton
from kivy.factory import Factory
import sqlite3
from kivy.uix.label import Label

class LoginRoot(BoxLayout):
    username = ObjectProperty()
    password = ObjectProperty()


    def login_form(self):
        username = (self.username.text)
        password = (self.password.text)
        f = open("text/name.txt", "w")
        f.write("%s" % username)
        f.close()
        f = open("text/pass.txt", "w")
        f.write("%s" % password)
        f.close()

    def log_form(self):
        print ("Hi")

    def id_form(self):
        self.clear_widgets()
        current_weather = Factory.Code()
        self.add_widget(current_weather)
        conn = sqlite3.connect('db/test')
        c = conn.cursor()
        se = open("text/name.txt", "r")
        username = se.read()
        c.execute("SELECT password from userin where username = '%s'" % username)
        se.close()
        d = c.fetchone()
        f = open("yes.txt", "w")
        f.write("%s" % d)
        f.close()
        d = open("yes.txt", "r")
        d = d.read()
        ser = open("text/pass.txt", "r")
        password = ser.read()
        if d == password:
            usernow = ("%s" % username)
            print usernow



class DisocialApp(App):

    pass

class Code(BoxLayout):
idcode = ObjectProperty()

def ids_form(self):
     print (self.idcode.text)


if __name__ == "__main__":
    DisocialApp().run()

Disocial.kv     LoginRoot:

<LoginRoot>:
    orientation: "vertical"
    username: user_input
    password: pass_input
    BoxLayout:
        height: "40dp"
        size_hint_y: None
        TextInput:
            id: user_input
            size_hint_x: 50
            focus: True
            multiline: False
        TextInput:
            id: pass_input
            size_hint_x: 50
            focus: True
            multiline: False
        Button:
            text: "Login"
            size_hint_x: 25
            on_press: root.login_form()
        Button:
            text: "Check Places"
            size_hint_x: 25
            on_press: root.id_form()

<Code@BoxLayout>:
    orientation: "vertical"
    idcode: id_input
    BoxLayout:
        height: "40dp"
        size_hint_y: None
        TextInput:
            id: id_input
            size_hint_x: 50
            focus: True
            multiline: False
        Button:
            text: "Login"
            size_hint_x: 25
            on_press: app.root.ids_form()

我知道得到这个错误;

File "/home/seandowney/PycharmProjects/SchoolShow/disocial.kv", line 43, in <module>
     on_press: app.root.ids_form()
 AttributeError: 'LoginRoot' object has no attribute 'ids_form'

我想要的是从(self.idcode.text)输入文本并打印出来。

我尝试了多项但无济于事,我感到非常困惑。 我相信     idcode = ObjectProperty() 应该像用户名和密码?

2 个答案:

答案 0 :(得分:1)

  

idcode = ObjectProperty()

这是您为该课程设置idcode的唯一地方。默认值为None,因此您得到给定的错误。

您确实设置了Code类的idcode属性,但从不访问它。我不确定你认为应该如何访问它,请详细说明如果你还不清楚什么是错的话,你认为应该发生什么。

答案 1 :(得分:0)

你在kv文件中定义类似的东西

<RootLogin>
     ....
<Code@BoxLayout>
     idcode = ...

这与

大致相同
class RootLogin:
     ...
class Code(BoxLayout):
     idcode = ...

那么为什么你认为你可以做到这一点

a = RootLogin()
a.idcode    #idcode is an attribute of Code ... not RootLogin

我不确定你对这些东西的期望是什么,所以我不能提供更多的帮助