from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivy.core.window import Window
from kivymd.uix.textfield import TextInput
from kivymd.uix.button import MDFillRoundFlatButton
import sqlite3
from kivymd.uix.list import OneLineListItem
from kivy.clock import Clock
con = sqlite3.connect('my.db')
c = con.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS post(
title TEXT NOT NULL,
main_text TEXT NOT NULL)""")
con.commit()
con.close()
Window.size = (412, 915)
Builder.load_string("""
<ScreenManager>:
MenuScreen:
AddPost:
ReadPost:
id: read_post
<MenuScreen>:
MDBoxLayout:
orientation: "vertical"
MDToolbar:
type: "top"
title: "Скорочтение"
MDBoxLayout:
orientation: "vertical"
padding:200, 550, 200, 550
spacing:25
MDRoundFlatIconButton:
text: "Добавить запись"
width: root.width*0.6
font_size: 40
icon: "plus"
on_release: root.manager.current = 'add'
MDRoundFlatIconButton:
text: "Читать на скорость"
width: root.width*0.6
font_size: 40
icon: "clock"
on_press: root.manager.current = 'read'
MDRoundFlatIconButton:
width: root.width*0.6
font_size: 40
icon: "eye"
text: "Внимательность"
<AddPost>:
MDBoxLayout:
orientation: "vertical"
MDToolbar:
type: "top"
title: "Добавить запись"
left_action_items: [['keyboard-backspace', lambda x: app.set_screen("menu")]]
MDBoxLayout:
orientation: "vertical"
padding:100, 200, 100, 600
spacing:45
MDTextField:
id: title
hint_text:"Описание"
pos_hint:{'center_x':.5,'center_y':.5}
font_size:"18dp"
line_color: "white"
width: 350
on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
MDTextField:
id: main_text
hint_text:"Основной текст"
pos_hint:{'center_x':.5}
font_size:"18dp"
line_color: "white"
multiline: True
size_hint: (1.0, None)
max_height: 500
on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
MDRoundFlatButton:
width: root.width * 0.75
font_size: "18dp"
text: "Сохранить"
halign: "center"
on_press: app.submit(root.ids.title.text, root.ids.main_text.text)
<ReadPost>:
ScrollView:
MDBoxLayout:
orientation: "vertical"
MDToolbar:
type: "top"
title: "Читать текст"
left_action_items: [['keyboard-backspace', lambda x: app.set_screen("menu")]]
# MDBoxLayout:
# orientation: "vertical"
ScrollView:
MDList:
id: container
"""
)
class MenuScreen(Screen):
pass
class AddPost(Screen):
pass
class ReadPost(Screen):
def __init__(self, **kw):
Clock.schedule_once(self.create_list)
super().__init__(**kw)
def create_list(self, *args):
con = sqlite3.connect("my.db")
c = con.cursor()
data = """SELECT * FROM post"""
c.execute(data)
post = c.fetchall()
print(post)
for i in post:
self.ids.container.add_widget(
OneLineListItem(text=f"{i}")
)
con.close()
print("das")
class MainApp(MDApp):
def set_screen(self, name):
self.root.current = name
print("ss")
def build(self):
sm = ScreenManager()
sm.add_widget(MenuScreen(name="menu"))
sm.add_widget(AddPost(name="add"))
sm.add_widget(ReadPost(name="read"))
return sm
def submit(self, title, main_text):
if title == '' or main_text == '':
print('eror')
else:
con = sqlite3.connect("my.db")
c = con.cursor()
c.execute("""INSERT INTO post(
title, main_text)
VALUES (?,?);""",
(title, main_text))
con.commit()
con.close()
print("Э")
MainApp().run()
Дело в том что в окне AddPost я загружаю данные в бд, а в окне ReadPost. нужно добавить новый List который я загрузил, каким способом можно сделать?