Hello.
I have a problem getting sqlite3 to save the data i write into a textinput within a popup.
How do I reference the textinput id from root of a screen in kvfile to make a variable when popup also needs to be root?
#### main.py ########################
import kivy
from
kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
import sqlite3 as sql
kivy.require('1.11.1')
class WindowManager(ScreenManager):
pass
class MainWindow(Screen):
pass
class TestWindow(Screen, FloatLayout):
test_input = ObjectProperty(None)
def test_save(self):
connection = sql.connect('test.db')
cursor = connection.cursor()
cursor.execute(""" INSERT INTO cleaning VALUE (?)""", self.test_input.text)
connection.commit()
connection.close()
class TestPopup(Popup):
pass
GUI = Builder.load_file('main_window.kv')
class MyApp(App):
test_win = TestWindow()
connection = sql.connect('test.db')
cursor = connection.cursor()
cursor.execute(""" CREATE TABLE IF NOT EXISTS cleaning(
test TEXT)
""")
connection.commit()
connection.close()
def build(self):
return GUI
def change_screen(self, screen_name):
sm = self.root.ids['sm']
sm.current = screen_name
if __name__ == '__main__':
MyApp().run()
#### main_window.kv ########################
#:include test_window.kv
#: import Factory kivy.factory.Factory
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
BoxLayout:
WindowManager:
id: sm
transition: FadeTransition()
MainWindow:
name: 'main'
TestWindow:
name: 'test'
<MainWindow>
name: 'main'
id: main
FloatLayout:
size: root.width, root.height
RoundedButton:
id: test_btn
text: "TEST"
color: (0,0,0,1)
size_hint: 0.2, 0.05
text_size: root.width, None
font_size: root.height/45
halign: "center"
pos_hint:{"x":0.4, "y":0.66}
on_press:
app.change_screen('test')
RoundedButton:
id: quit_btn
text: "QUIT"
color: (0,0,0,1)
size_hint: 0.2, 0.05
text_size: root.width, None
font_size: root.height/45
halign: "center"
pos_hint:{"x":0.4, "y":0.32}
on_press:
app.stop()
<RoundedButton@Button>
background_color: (1,7,1,0)
background_normal: ''
canvas.before:
Color:
rgba: (0,1,1,1)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [22]
#### test_window.kv ########################
#: set black 0, 0, 0, 1
#: set red 1, 0, 0, 1
#: set gray .4, .4, .4, 1
#: set blue 0, 0, 1, 1
FloatLayout:
######this######
## test_input = test_popup_input <== How do I access this id from here? ##
WindowManager:
id: sm
TestWindow:
name: 'test'
id: test
<TestWindow>
name: 'test'
id: test
TabbedPanel:
do_default_tab:False
tab_pos: 'top_mid'
background_color: (0, 0, 0, 1)
ColorTabbedPanelItem:
normal_color: black
down_color: blue
text: "TEST"
font_size: 14
color: 1,1,1,1
FloatLayout:
Label:
text: "TEXT : "
pos_hint:{"center_x": 0.2, "center_y": 0.5}
Label:
id: test_popup_task
text: "" ##### <== root.test_save() ######
pos_hint:{"center_x": 0.6, "center_y": 0.5}
RoundedButton:
text: "INPUT"
color: (0,0,0,1)
size_hint: 0.06, 0.08
text_size: root.width, None
font_size: root.height/45
halign: "center"
pos_hint: {"x":0.75, "y":0.46}
on_press:
Factory.TestPopup().open()
RoundedButton:
text: "BACK"
color: (0,0,0,1)
size_hint: 0.2, 0.05
text_size: root.width, None
font_size: root.height/45
halign: "center"
pos_hint:{"x":0.4, "y":0.05}
on_release:
app.change_screen('main')
<ColorTabbedPanelItem@TabbedPanelItem>
normal_color: gray
down_color: red
background_color: {'normal': root.normal_color, 'down': root.down_color} [self.state]
background_down: ''
background_normal: ''
<TestPopup@Popup>
auto_dismiss:True
size_hint: 0.6, 0.21
title: "WRITE SOMETHING"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
TextInput:
id: test_popup_input
color: (0,1,0,1)
size_hint: 1, 0.04
multiline:True
RoundedButton:
id: test_popup_save
text: "SAVE"
color: (0,0,0,1)
font_size: 14
size_hint: 0.37, 0.04
pos_hint:{"x":0.3, "y":0.05}
on_press:
app.root.ids.sm.get_screen('test').ids.test_popup_task.text=test_popup_input.text
on_release:
app.test_win.test_save()
root.dismiss()
<RoundedButton@Button>
background_color: (1,7,1,0)
background_normal: ''
canvas.before:
Color:
rgba: (0,1,1,1)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [22]