我在.py文件中创建了一个TextInput小部件,我正在尝试访问TextInput的值,因此我可以将它用于Sqlite3查询。出于某种原因,我不断收到错误说" AttributeError:' NoneType'对象没有属性' text'"。
如果我在.kv文件中创建窗口小部件并使用id作为ObjectProperty(),我可以访问TextInput_text值。我不确定我是否必须在.py文件中执行类似的操作。
这是我正在尝试的代码:
def choose_date_water(self):
box = FloatLayout()
box.add_widget(Label(text = "Select Date To View", font_size = (30), pos_hint = {'center_x':0.5, 'center_y': 0.9 }))
self.dp1 = box.add_widget(TextInput(pHint_x = (0.35), pHint_y = (0.55), size_hint = (None, None), size = (190, 50), font_size = (33), pos_hint = {'center_x':0.5, 'center_y': 0.6 }))
btn1 = Button(text = "OK", size_hint = (None, None), size = (200, 50), pos_hint = {'center_x':0.5, 'center_y': 0.25 })
box.add_widget(btn1)
popup1 = Popup(title = "Choose Date", title_size = (40), title_align = 'center', content = box, size_hint = (None, None), size = (600, 300))
btn1.bind(on_press = self.view_water_figures, on_release = popup1.dismiss)
popup1.open()
return self.dp1
def view_water_figures(self, instance):
conn = sqlite3.connect('logsheet.db')
c = conn.cursor()
c.execute("SELECT today_total_dw_vol, today_total_fw_vol, total_evap_out FROM waterfigures WHERE date = ?", (self.dp1.text,))
wf = c.fetchall()
print wf
任何帮助都会很棒。
感谢。
答案 0 :(得分:1)
c.execute("SELECT today_total_dw_vol, today_total_fw_vol, total_evap_out FROM waterfigures WHERE date = ?", (self.dp1.text,))
AttributeError: 'NoneType' object has no attribute 'text'
self.dp1
是一个ObjectProperty。 print(type(self.dp1))
会显示它是<class 'NoneType'>
的类型。它没有与TextInput小部件挂钩。解决方案如下:
def choose_date_water(self):
...
self.dp1 = TextInput(pHint_x = (0.35), pHint_y = (0.55), size_hint = (None, None), size = (190, 50), font_size = (33), pos_hint = {'center_x':0.5, 'center_y': 0.6 })
box.add_widget(self.dp1)
id: sel_date
在Python代码中,使用self.ids
look对象来访问kv文件中定义的TextInput文本。
self.ids.sel_date.text
Accessing Widgets defined inside Kv lang in your python code
解析你的kv文件后,kivy会收集所有标记的小部件 id并将它们放在此
中self.ids
字典类型属性
详情请参阅以下示例。
import sqlite3
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
class SQLite3KivyDemo(BoxLayout):
def db_query(self):
connection = sqlite3.connect("company.db")
cursor = connection.cursor()
sql_command = """SELECT * FROM employee WHERE birth_date = "{}";""".format(self.ids.sel_date.text)
cursor.execute(sql_command)
result = cursor.fetchall()
for row in result:
print(row)
class TestApp(App):
def build(self):
Config.set("graphics", "width", "800")
Config.set("graphics", "height", "50")
Config.set("graphics", "borderless", "0")
Config.set("graphics", "resizable", "0")
self.title = "Kivy & SQLite3 Demo"
return SQLite3KivyDemo()
if __name__ == "__main__":
TestApp().run()
#:kivy 1.10.0
<SQLite3KivyDemo>:
canvas.before:
Color:
rgb: 1, 0.5, 0 # Orange
# rgb: 0, 0.5, 1 # Sky blue
Rectangle:
pos: self.pos
size: self.size
orientation: "vertical"
BoxLayout:
size_hint_y: 0.1
Label:
text: "Enter Date of Birth"
TextInput:
id: sel_date
multiline: False
size_hint_x: 3
Button:
text: "Display Data"
on_release: root.db_query()