KIVY - 文本输入 .kv 错误
这是我的代码:
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 个回答
1
idcode = ObjectProperty()
这是你为这个类设置idcode的唯一地方。默认值是None,所以你会遇到那个错误。
你确实设置了Code类的idcode属性,但从来没有去访问它。我不太清楚你认为应该是谁在访问它,如果你还是不明白哪里出错了,请详细说明一下你认为应该发生什么。
0
你在你的kv文件中定义的东西大概是这样的
<RootLogin>
....
<Code@BoxLayout>
idcode = ...
这大致相当于
class RootLogin:
...
class Code(BoxLayout):
idcode = ...
那么你为什么会觉得你可以这样做呢
a = RootLogin()
a.idcode #idcode is an attribute of Code ... not RootLogin
我不太清楚你希望这些东西能做什么,所以我帮不了你更多。