问题描述
我已经在我的.py文件中创建了TextInput小部件,并且尝试访问TextInput的值,以便可以将其用于Sqlite3查询.由于某种原因,我一直收到错误消息,提示"AttributeError:'NoneType'对象没有属性'text'".
I have created a TextInput widget in my .py file and i am trying to access the value of the TextInput so i can use it for an Sqlite3 query. For some reason i keep getting an error saying "AttributeError: 'NoneType' object has no attribute 'text'".
如果我在.kv文件中创建窗口小部件并将ID用作ObjectProperty(),则能够访问TextInput_text值.我不确定是否必须在.py文件中执行类似的操作.
I am able to access TextInput_text values if i create the widget in the .kv file and use the id as an ObjectProperty(). I am unsure if i have to do something like that inside the .py file.
这是我正在尝试的代码:
Here is the code i am trying:
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
任何帮助都会很棒.
谢谢.
推荐答案
AttributeError-说明
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小部件.解决方法如下:
self.dp1
is an ObjectProperty. A print(type(self.dp1))
will show that it is a type of <class 'NoneType'>
. It is not hook up to TextInput widget. The solution is as follow:
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)
解决方案-使用Python和Kv文件
- 在kv文件中,为TextInput小部件提供一个ID,例如
id: sel_date
-
在Python代码中,使用
self.ids
look对象访问kv文件中定义的TextInput文本.
- In kv file, give an id to the TextInput widget e.g.
id: sel_date
In Python code, use
self.ids
look object to access the TextInput's text defined in kv file.
self.ids.sel_date.text
self.ids.sel_date.text
在您的python代码中访问在Kv lang内部定义的小部件
有关详细信息,请参见以下示例.
Please refer to the example below for details.
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()
test.kv
#: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()
输出
这篇关于如何从.py文件访问Kivy TextInput'文本'属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!